2016-07-15 27 views
3

我們有一個獨家類別X和其他常規類別Y。我想什麼:WooCommerce購物車 - 條件項目類別驗證

  • 當有人訂單X類什麼,其他類別的物品不能添加到購物車,並應顯示警告
  • Y類產品不應與X混合。

我怎麼能做到這一點?

我從其他崗位這段代碼,但它已經過時,不能令人滿意:

<?php 

// Enforce single parent category items in cart at a time based on first item in cart 
function get_product_top_level_category ($product_id) { 

    $product_terms   = get_the_terms ($product_id, 'product_cat'); 
    $product_category   = $product_terms[0]->parent; 
    $product_category_term = get_term ($product_category, 'product_cat'); 
    $product_category_parent = $product_category_term->parent; 
    $product_top_category  = $product_category_term->term_id; 

    while ($product_category_parent != 0) { 
      $product_category_term = get_term ($product_category_parent, 'product_cat'); 
      $product_category_parent = $product_category_term->parent; 
      $product_top_category  = $product_category_term->term_id; 
    } 

    return $product_top_category; 
} 

add_filter ('woocommerce_before_cart', 'restrict_cart_to_single_category'); 
function restrict_cart_to_single_category() { 
    global $woocommerce; 
    $cart_contents = $woocommerce->cart->get_cart(); 
    $cart_item_keys = array_keys ($cart_contents); 
    $cart_item_count = count ($cart_item_keys); 

    // Do nothing if the cart is empty 
    // Do nothing if the cart only has one item 
    if (! $cart_contents || $cart_item_count == 1) { 
      return null; 
    } 

    // Multiple Items in cart 
    $first_item     = $cart_item_keys[0]; 
    $first_item_id     = $cart_contents[$first_item]['product_id']; 
    $first_item_top_category  = get_product_top_level_category ($first_item_id); 
    $first_item_top_category_term = get_term ($first_item_top_category, 'product_cat'); 
    $first_item_top_category_name = $first_item_top_category_term->name; 

    // Now we check each subsequent items top-level parent category 
    foreach ($cart_item_keys as $key) { 
      if ($key == $first_item) { 
        continue; 
      } 
      else { 
        $product_id   = $cart_contents[$key]['product_id']; 
        $product_top_category = get_product_top_level_category($product_id); 

        if ($product_top_category != $first_item_top_category) { 
          $woocommerce->cart->set_quantity ($key, 0, true); 
          $mismatched_categories = 1; 
        } 
      } 
    } 

    // we really only want to display this message once for anyone, including those that have carts already prefilled 
    if (isset ($mismatched_categories)) { 
      echo '<p class="woocommerce-error">Only one category allowed in cart at a time.<br />You are currently allowed only <strong>'.$first_item_top_category_name.'</strong> items in your cart.<br />To order a different category empty your cart first.</p>'; 
    } 
} 
?> 

感謝

回答

3

像一切都在你的專屬類別category X轉彎,你需要使用條件爲這個類別。

而你有機會,因爲有一種特殊功能可以與woocommerce產品類別結合使用。假設**cat_x**是您獨家類別的slu as,因爲您知道它product_cat是獲取產品類別的參數。

has_term()條件函數

所以,你要使用這樣的:

if (has_term ('cat_x', 'product_cat', $item_id)) { // or $product_id 
    // doing something when product item has 'cat_x' 
} else { 
    // doing something when product item has NOT 'cat_x' 
} 

我們需要在foreach循環運行車的物品2次:

  • 要檢測是否存在在該車中是cat_x項目。
  • 要刪除其他項目,如果cat_x檢測到購物車中的一個物品並激發正確的消息。

在下面的代碼中,我已經更改爲更有用的鉤子。這個鉤子將檢查你的購物車中有什麼。這個想法是在購物車中添加'cat_x'項目時,刪除購物車中的其他類別物品。

代碼評論很好。最後你會發現不同的通知被觸發。你需要把你的真實文本放在每個文本中。

add_action('woocommerce_check_cart_items', 'checking_cart_items'); 
function checking_cart_items() { 

    global $woocommerce; // if needed, not sure (?) 
    $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 woocommerce 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'); 
     } 
    } 
}  

我真的測試此代碼是不可能的(但它不會引發錯誤)......


@edit

我們可以用比通知別的東西...一切皆有可能。但這是一個很好的開始解決方案,可以進行微調。

你需要通過你的真實類別蛞蝓(一開始)更換'cat_x' ...

+0

看起來像我得到了解決,非常感謝。 –

+0

@BluecupsSolution只是歡迎:) ...我不完全確定這個回答。如果您需要更多信息,請在這裏評論我。謝謝 – LoicTheAztec

相關問題