2017-07-15 38 views
1

我正在使用WooCommerce創建在線食品交付網站,並且需要創建一個「使其成爲餐」類型upsell供客戶購買。根據產品類別有條件地操作WooCommerce購物車項目

我已經創建了一款名爲「Make it a Meal」的產品,用戶可以在其中選擇飲料和他們選擇的一面(使用變體)。然而,我需要的是這些產品中只有一種能夠在每頓主餐中購買。

例子:

  • 產品中的「主餐」類別添加到購物車
  • 用戶可以再加入「讓它吃飯」的產品到購物車
  • 用戶不能添加第二個「使它成爲一個膳食「產品,除非他們有」膳食餐「類別​​中的第二種產品,即每種產品只有一種產品
  • 如果從購物車中取出」主餐「產品,則」去它做飯「也會被移除。

以前有人有過這種功能嗎?我嘗試了各種插件,但目前沒有運氣。

回答

0

更新時間:

有可能首先檢查上添加到購物車事件(可選顯示錯誤通知):

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

    $cat_add = 'Make it a Meal'; 
    $cat_src = 'Main Meal'; 

    if(has_term($cat_add, 'product_cat', $product_id)): 

     $main_meal_count = 0; 
     $make_it_a_meal_count = 0; 

     foreach (WC()->cart->get_cart() as $cart_item){ 

      // Counting 'Main Meal' products in cart (with quantity) 
      if(has_term($cat_src, 'product_cat', $cart_item['product_id'])) 
       $main_meal_count += $cart_item['quantity']; 

      // Counting 'Make it a Meal' products in cart (with quantity) 
      if(has_term($cat_add, 'product_cat', $cart_item['product_id'])) 
       $make_it_a_meal_count += $cart_item['quantity']; 
     } 

     if($main_meal_count < ($make_it_a_meal_count + $quantity)) { 
      $passed = false; 

      // Displaying a message (optionnal) 
      wc_add_notice('my custom error message…', 'error'); 
     } 
    endif; 

    return $passed; 
} 

然後,您還需要檢查時,車量改變或購物車物品刪除:

add_action('woocommerce_calculate_totals', 'check_removed_cart_items', 10, 1); 
function check_removed_cart_items($cart_object) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    $cat_add = 'Make it a Meal'; 
    $cat_src = 'Main Meal'; 

    $main_meal_count = 0; 
    $make_it_a_meal_count = 0; 

    // First loop: Counting cart items 'Main Meal' and 'Make it a Meal' 
    foreach ($cart_object->get_cart() as $item_values){ 

     // Counting 'Main Meal' products in cart (with quantity) 
     if(has_term($cat_src, 'product_cat', $item_values['product_id'])) 
      $main_meal_count += $item_values['quantity']; 

     // Counting 'Make it a Meal' products in cart (with quantity) 
     if(has_term($cat_add, 'product_cat', $item_values['product_id'])){ 
      $make_it_a_meal_count += $item_values['quantity']; 
     } 
    } 

    $difference = intval($make_it_a_meal_count - $main_meal_count); 

     echo '<p>$main_meal_count is '.$main_meal_count.'</p>'; 
     echo '<p>$make_it_a_meal_count is '.$make_it_a_meal_count.'</p>'; 
     echo '<p>$difference is '.$difference.'</p>'; 

    if($main_meal_count < $make_it_a_meal_count) { 

     echo '<p>case1</p>'; 


     // Second Loop: Make necessary actions 
     foreach ($cart_object->get_cart() as $cart_item_key => $cart_item){ 

      // Targeting 'Make it a Meal' 
      if(has_term($cat_add, 'product_cat', $cart_item['product_id'])){ 

       $item_qty = intval($cart_item['quantity']); 

       if($item_qty == 1 && $difference == 1){ 
        $cart_object->remove_cart_item($cart_item_key); 
        break; 
       } else { 
        if($item_qty > 1 && $difference <= $item_qty){ 
         $cart_object->set_quantity($cart_item_key, ($item_qty - $difference)); 
         break; 
        } else { 
         $cart_object->remove_cart_item($cart_item_key); 
        } 
       } 
      } 
     } 
    } 
} 

有了這2個掛鉤功能,你有一個完整的解決方案。

代碼會出現在您的活動子主題(或主題)的function.php文件中,或者也存在於任何插件文件中。

此代碼已經過測試並可正常工作。

+0

非常感謝你!這對於「簡單產品」很有用。你知道如何讓它與「變量產品」一起工作嗎? – user2766888

+0

只是爲了確認,WooCommerce通知正確顯示變量產品(即XXX產品已添加到購物車或我的自定義錯誤消息)。但是當我查看購物車時,變量產品未成功添加。 – user2766888

相關問題