如果在Woocommerce中购物车是空的,请重定向到首页 [英] Redirect to home if the cart is empty in Woocommerce

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

问题描述

我正在寻找并且找不到任何地方,因为我可以将空的woocommerce购物车重定向到首页。我只找到进入商店的重定向。

I am looking for and can not find anywhere as I can redirect the empty woocommerce cart to the homepage. I only find redirects that go to the store.

这是我发现的内容,但我无需重定向到家:

This is what I find, but I do not need to redirect to the home:

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' ) ) );
    }
}

上下文链接:如果购物车为空,购物车页面将重定向到woocommerce中的商店页面?

谢谢

推荐答案

如果购物车为空,则要重定向到首页,您将使用类似的代码:

To redirect to home if cart is empty you will use a similar code:

add_action( 'template_redirect', 'empty_cart_redirection' );
function empty_cart_redirection(){
    if( WC()->cart->is_empty() && ! ( is_front_page() || is_cart() ) ){
        wp_safe_redirect( esc_url( home_url( '/' ) ) );
        exit;
    }
}

代码会出现在您活跃孩子的function.php文件中主题(活动主题)。经过测试并可以正常工作。

Code goes in function.php file of your active child theme (active theme). Tested and works.


但是,如果通过ajax清空购物车,则此代码将启用重定向到主页的操作,直到重新加载页面为止。

But if cart is emptied through ajax, this code will enable a redirection to home until the page will be reloaded.

这篇关于如果在Woocommerce中购物车是空的,请重定向到首页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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