2017-08-06 44 views
0

在WooCommerce,我自動應用優惠券,但我不能找到一個辦法讓這種行爲:WooCommerce:自動應用相同的優惠券幾次

  • 每次X項目在購物車=>申請適當的優惠券

例如:

  • 如果筆者=> -5€上車的3本書
  • 如果額外的(同樣是其他人)同一作者3本書=> -5額外€
  • 等(沒有限制:它應該工作,如果3000名的書籍訂購=> -15000€)

我使用$ WC-> cart-> add_discount($折扣),但它已返回」優惠券應用「作爲第二個g一組物品在購物車中。

你知道這是可能嗎?

感謝

回答

1

而不是使用優惠券,你應該更好地使用自定義功能折讓得到這個工作:

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

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

    // initializing and set variables 
    $discount = 0; 
    $by3 = 3; // each 3 item quantity 
    $dicount_price_by3 = 5; // amout to discount each 3 items quantity 

    // Iterating through each cart item 
    foreach($cart_object->get_cart() as $cart_item): 
     // Get the item quantity 
     $qty = $cart_item["quantity"]; 
     // starting when quantity is upto 3 
     if($qty >= $by3): 
      for($j = $by3, $k = 0; $j <= $qty; $j+=$by3, $k++); 
      $discount += $dicount_price_by3 * $k; 
      break; 
     endif; 
    endforeach; 

    // Adding the discount (a negative fee) 
    if ($discount > 0){ 
     $cart_object->add_fee(__("Discount quantity", 'woocommerce'), -$discount, true); 
     # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 

     // Displaying a custom notice (optional) 
     wc_clear_notices(); 
     wc_add_notice(__("You get a quantity discount on some items"), 'notice'); 
    } 
} 

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

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

+0

謝謝!它的工作原理:)而且我還在研究另一種解決方案,它的詳細程度並不高。 –