首页 文章

Woocommerce - 将特定类别的产品添加到购物车时清空购物车?

提问于
浏览
1

在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;
  }

1 回答

  • 1

    找到了解决方案 . 与我尝试过的其他解决方案相比,不确定为什么这样可行,但它确实有效!

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

相关问题