2016-12-06 65 views
4

我有非常具體的項目,我需要一些不同的購物車規則。我無法找到插件或任何其他資源如何實現這一目標。WooCommerce - 基於子類別的條件購物車計算

我有子類別1(即表格)和子類別2(即椅子)。用戶只能從子類別表中添加1個產品,這是強制性的,並且從子類別主題中選擇了多少產品,但這不是強制性的。

我需要一個規則:如果用戶還添加了產品從子類別主席然後從子類別表產品減去子類別主席的總價產品。同樣在這種情況下,如果價格將爲< 0,那麼將價格設置爲0.

有沒有人有任何想法如何使用標準Wordpress Woocommerce來做到這一點?

回答

1

這有可能使這項工作,將根據您的要求,子類別和計算車的折扣......

代碼:

add_action('woocommerce_cart_calculate_fees','table_chairs_cart_discount', 10, 1); 
function table_chairs_cart_discount($cart_object) { 

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

    // Initializing variables 
    $chairs_total = 0; 
    $table_total = 0; 
    $discount = 0; 

    // Iterating through each cart item 
    foreach($cart_object->get_cart() as $item_key => $item): 

     $item_line_total = $item["line_total"]; // Item total price (price x quantity) 

     // Chairs subcategory items 
     if(has_term('chairs', 'product_cat', $item['product_id'])) 
      $chairs_total += $item_line_total; 

     // Table subcategory items 
     if(has_term('table', 'product_cat', $item['product_id'])) 
      $table_total += $item_line_total; 

    endforeach; 

    // ## CALCULATIONS ## 
    if($table_total <= $chairs_total && $chairs_total > 0) 
     $discount -= $table_total; 
    elseif ($chairs_total > 0) 
     $discount -= $chairs_total; 

    // Adding the discount 
    if ($discount != 0) 
     $cart_object->add_fee(__('Chairs discount', 'woocommerce'), $discount, false); 
     // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 
} 

代碼放在您的活動子主題的function.php文件(或主題)。或者也可以在任何插件php文件中使用。


相關答案:Discount for Certain Category Based on Total Number of Products

+0

非常感謝完美的作品。 [大擁抱] :) –

相關問題