2017-02-22 120 views
3

我在購物車中添加特殊商品類別'cat_x'的商品時,使用以下代碼從商品中移除其他WooCommerce商品類別商品並顯示一些不同的自定義商品注意事項。該代碼來自this thread,只是效果很好:限制購物車商品來自同一產品類別

add_action('woocommerce_check_cart_items', 'checking_cart_items'); 
function checking_cart_items() { 
    $special = false; 
    $catx = 'cat_x'; 
    $number_of_items = sizeof(WC()->cart->get_cart()); 

    if ($number_of_items > 0) { 

     // Loop through all cart products 
     foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
      $item = $values['data']; 
      $item_id = $item->id; 

      // detecting if 'cat_x' item is in cart 
      if (has_term($catx, 'product_cat', $item_id)) { 
       if (!$special) 
        $special = true; 
      } 
     } 

     // Re-loop through all cart products 
     foreach (WC()->cart->get_cart() as $cart_item_key => $values) { 
      $item = $values['data']; 
      $item_id = $item->id; 

      if ($special) // there is a 'cat_x' item in cart 
      { 
       if ($number_of_items == 1) { // only one 'cat_x' item in cart 
        if (empty($notice)) 
         $notice = '1'; 
       } 
       if ($number_of_items >= 2) { // 'cat_x' item + other categories items in cart 
      // removing other categories items from cart 
        if (!has_term($catx, 'product_cat', $item_id)) { 
         WC()->cart->remove_cart_item($cart_item_key); // removing item from cart 
         if (empty($notice) || $notice == '1') 
          $notice = '2'; 
        } 
       } 
      } else { // Only other categories items 
       if (empty($notice)) 
        $notice = '3'; 
      } 
     } 

     // Firing notices 
     if ($notice == '1') { // message for an 'cat_x' item only (alone) 
      wc_add_notice(sprintf('<p class="woocommerce-error">bla bla bla one category X item in the cart</p>'), 'success'); 
     } elseif ($notice == '2') { // message for an 'cat_x' item and other ones => removed other ones 
      wc_add_notice(sprintf('<p class="woocommerce-error">bla bla bla ther is already category X in the cart => Other category items has been removed</p>'), 'error'); 
     } elseif ($notice == '3') { // message for other categories items (if needed) 
      wc_add_notice(sprintf('<p class="woocommerce-error">bla bla bla NOT category X in the cart</p>'), 'success'); 
     } 
    } 
} 

具有條件函數has_term()與類別的陣列也工作,我已經試過,而不是一個類別,設置類別的數組中的代碼。 但它不起作用

但是,我的需求發生了變化:我不想讓客戶有可能從不同類別中選擇購物車物品。所以購物車必須始終有來自同一產品類別的物品。

請幫忙嗎?

謝謝。

回答

3

在woocommerce_add_to_cart_validation過濾器掛鉤中創建一個自定義函數將以更簡單的方式完成工作,而無需設置任何類別數組。

因此,您的代碼將更加快速和緊湊。此外,您可以顯示自定義通知來警告客戶。

此代碼將避免添加到購物車,如果不同類別的項目在購物車:

add_filter('woocommerce_add_to_cart_validation', 'custom_checking_product_added_to_cart', 10, 3); 
function custom_checking_product_added_to_cart($passed, $product_id, $quantity) { 

    // HERE Type your alert displayed message 
    $message = __('BLA BLA YOUR MESSAGE.', 'woocommerce'); 

    $product_cats_object = get_the_terms($product_id, 'product_cat'); 
    foreach($product_cats_object as $obj_prod_cat){ 
     $product_cats[] = $obj_prod_cat->slug; 
    } 

    foreach (WC()->cart->get_cart() as $cart_item){ 
     if(! has_term($product_cats, 'product_cat', $cart_item['product_id'])) { 
      $passed = false; 
      wc_add_notice($message, 'error'); 
      break; 
     } 
    } 
    return $passed; 
} 

代碼放在您的活動子主題(或主題)的function.php文件或還任何插件文件。

此代碼測試,它的工作原理


限制車中的物品,從不同的產品類別只能是:

在功能更換條件:

if(! has_term($product_cats, 'product_cat', $cart_item['product_id'])) { 

通過

if(has_term($product_cats, 'product_cat', $cart_item['product_id'])) { 
+0

謝謝你,這個解決方案完美的作品。 –

相關問題