在Woocommerce中,当特定类别的产品添加到购物车时,如何让购物车清空?
我发现以下代码会在任何产品添加到购物车时清空购物车,但我需要此选项仅在产品来自特定类别时应用:
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
答案 0 :(得分:2)
找到解决方案。与我尝试过的其他解决方案相比,不确定为什么这样可行,但它确实有效!
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id ) {
global $woocommerce;
//Check if product ID is in a certain category
if( has_term( 'category-slug', 'product_cat', $product_id ) ){
$woocommerce->cart->empty_cart();
}
//Do nothing with the data and return
return $cart_item_data;
}