2017-02-09 71 views
3

我已经成立了一家Woocommerce商店,并希望根据12(盒子)的倍数设置所有商品的特定折扣。我已经尝试了很多折扣插件,但没有找到我要找的东西。根据商品数量有条件地添加折扣商品

例如,如果我订购了12件产品X,我可以享受10%的折扣。如果我订购了15件产品X,我会在前12件商品中享受10%的折扣,而最后三件商品则是全价。如果我为了24,那么10%的折扣适用于产品X的所有24

我发现的最接近的是这样的:Discount for Certain Category Based on Total Number of Products

但这施加在一个折扣(实际上是负费)最后,我想在购物车旁边的购物车中显示折扣,例如正常折扣。

如果产品已经发售,我还需要禁用此折扣。

谢谢。

回答

3

是的,这也有可能,让每个车项目自定义计算和单独更换其价格(符合条件和计算),使用woocommerce_before_calculate_totals行动钩勾住了自定义的功能。

这是代码:

add_action('woocommerce_before_calculate_totals', 'custom_discounted_cart_item_price', 10, 1); 
function custom_discounted_cart_item_price($cart_object) { 

    $discount_applied = false; 

    // Set Here your targeted quantity discount 
    $t_qty = 12; 

    // Iterating through each item in cart 
    foreach ($cart_object->get_cart() as $item_values) { 

     ## Get cart item data 
     $item_id = $item_values['data']->id; // Product ID 
     $item_qty = $item_values['quantity']; // Item quantity 
     $original_price = $item_values['data']->price; // Product original price 

     // Getting the object 
     $product = new WC_Product($item_id); 


     // CALCULATION FOR EACH ITEM 
     // when quantity is up to the targetted quantity and product is not on sale 
     if($item_qty >= $t_qty && !$product->is_on_sale()){ 
      for($j = $t_qty, $loops = 0; $j <= $item_qty; $j += $t_qty, $loops++); 

      $modulo_qty = $item_qty % $t_qty; // The remaining non discounted items 

      $item_discounted_price = $original_price * 0.9; // Discount of 10 percent 

      $total_discounted_items_price = $loops * $t_qty * $item_discounted_price; 

      $total_normal_items_price = $modulo_qty * $original_price; 

      // Calculating the new item price 
      $new_item_price = ($total_discounted_items_price + $total_normal_items_price)/$item_qty; 


      // Setting the new price item 
      $item_values['data']->price = $new_item_price; 

      $discount_applied = true; 
     } 
    } 
    // Optionally display a message for that discount 
    if ($discount_applied) 
     wc_add_notice(__('A quantity discount has been applied on some cart items.', 'my_theme_slug'), 'success'); 
} 

本作正是您在车中的每个项目分别预计折扣(基于它的量),而不是对正在销售项目。但是,您不会得到任何标签(文本),表明购物车的订单项中有折扣。

可选我显示的通知时,折扣应用于一些购物车的物品......

代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。

此代码已经过测试并可正常工作。

+1

非常感谢你,这是完美的。 – Phovos

相关问题