如果此页面不是购物车或结账页面,我想在页面加载时清除购物车页面即使对于登录的用户和管理员,任何页面也会被清除。这段代码可以工作,但现在不行了
/**
* 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 );
}
Updated并增强。
使用 WooCommerce 条件标签并尝试template_redirect
改为钩子(当购物车不为空时):
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 );
}
}
代码位于活动子主题*(或插件中)的functions.php 文件中。它应该有效。
*避免使用父主题,因为当主题更新时,您的所有自定义设置都将被删除。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)