如果购物车为空,则购物车页面将重定向到WooCommerce中的商店页面? [英] If cart is empty, the cart page will redirect to shop page in WooCommerce?

查看:62
本文介绍了如果购物车为空,则购物车页面将重定向到WooCommerce中的商店页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,当购物车页面为空时,我想将购物车页面重定向到商店页面,否则显示购物车页面.谁能找到解决方案?

这是我尝试过的代码,但是它不起作用:

  function my_empty_cart(){全球$ woocommerce;如果(isset($ _GET ['empty-cart'])){wp_safe_redirect(get_permalink(woocommerce_get_page_id('product')));}}add_action('init','my_empty_cart'); 

解决方案

//旧的woocommerce:使用sizeof($ woocommerce-> cart-> cart_contents)检查购物车内容计数

//在新的woocommerce 2.1+中:WC()-> cart-> cart_contents_count检查购物车内容计数

  add_action("template_redirect",'redirection_function');函数redirection_function(){全球$ woocommerce;if(is_cart()&& WC()-> cart-> cart_contents_count == 0){wp_safe_redirect(get_permalink(woocommerce_get_page_id('shop')));}} 

init 挂钩将每次运行.使用 template_redirect

==============更新=============

在新的woocommerce中,他们已经更新了功能,现在您可以使用以下功能直接获取购物车内容计数.

WC()->购物车-> cart_contents_count

In WooCommerce, I want to redirect the cart page to shop page when the cart page is empty otherwise shows the cart page. Can anyone have the solution ?

Here is the code I have tried, but it does not work:

function my_empty_cart() {
  global $woocommerce;

    if (isset( $_GET['empty-cart'] ) ) { 
        wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'product' ) ) );
    }
}
add_action( 'init', 'my_empty_cart' );

解决方案

// old woocommerce : use sizeof( $woocommerce->cart->cart_contents) to check cart content count

// In new woocommerce 2.1+ : WC()->cart->cart_contents_count to check cart content count

add_action("template_redirect", 'redirection_function');
function redirection_function(){
    global $woocommerce;
    if( is_cart() && WC()->cart->cart_contents_count == 0){
        wp_safe_redirect( get_permalink( woocommerce_get_page_id( 'shop' ) ) );
    }
}

init hook will run everytime. use template_redirect

==============Updates=============

In new woocommerce, they have updated the functionality and now you can use following function to directly get the cart content count.

WC()->cart->cart_contents_count

这篇关于如果购物车为空,则购物车页面将重定向到WooCommerce中的商店页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆