2017-02-09 143 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

相關問題