wordpress - Woocommerce - Empty cart when products in a specific category are added to cart? -


how can cart empty when product specific category added cart, in woocommerce?

i found following code empties cart when product added cart, need apply when product specific category:

  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();     // nothing data , return    return $cart_item_data;   } 

found solution. not sure why works compared other solutions i've tried, works!

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 in category  if( has_term( 'category-slug', 'product_cat', $product_id ) ){    $woocommerce->cart->empty_cart();  }   //do nothing data , return  return $cart_item_data;  } 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -