即使已登录的用户也清除页面加载中的Woocommerce购物车 [英] Clear Woocommerce Cart on Page Load Even for logged in users

查看:94
本文介绍了即使已登录的用户也清除页面加载中的Woocommerce购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果此页面不是购物车页面或结帐页面,我想在页面加载时清除购物车页面.即使对于已登录的用户和管理员,也会清除所有页面.这段代码可以正常工作了,但是不再可用

I want to clear the cart page on page load if this page is not a cart or a checkout page Even for logged in users and admins, any page then it clears. This code was working but its not anymore

/**
 * Clears WC Cart on Page Load
 * (Only when not on cart/checkout page)
 */
 
add_action( 'wp_head', 'bryce_clear_cart' );
function bryce_clear_cart() {
if ( wc_get_page_id( 'cart' ) == get_the_ID() || wc_get_page_id( 'checkout' ) == get_the_ID() ) {
    return;
}
WC()->cart->empty_cart( true );
}

推荐答案

已更新并得到增强.

使用Woocommerce条件标签,并尝试使用 template_redirect 钩子代替(当购物车不为空时):

Use Woocommerce conditional tags and try template_redirect hook instead (when cart is not empty):

add_action( 'template_redirect', 'custom_empty_cart' );
function custom_empty_cart() {
    if ( ! ( is_cart() || is_checkout() ) && ! WC()->cart->is_empty() ) {
        WC()->cart->empty_cart( true );
}

代码在您的活动子主题(或活动主题)的function.php文件上.应该可以.

Code goes on function.php file of your active child theme (or active theme). It should work.

这篇关于即使已登录的用户也清除页面加载中的Woocommerce购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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