2016-12-06 67 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

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

相关问题