2017-08-06 12 views
0

を適用し、私は自動的にクーポンを適用していますが、私はこの動作を取得する方法を見つけることができません。WooCommerce:自動WooCommerceで同じクーポンを複数回

  • たびにXアイテムがカートにある=>適用されます適切なクーポン。例えば

  • カート
  • にこの著者=> -5€の3冊かの3冊の本は、余分な(同一の他のものである)場合、同じ著者=> -5€余分の
  • など(制限:それは作品は3000冊が発注されていないかどうかを=> -15000€)私は$ WC-> cart-> add_discount($割引)を使用するが、それはすでに」クーポンを返す

第2の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

ありがとうございます!それは完全に動作します:)そして、それは私が取り組んでいた別の解決策ではあまり冗長ではありません。 –

関連する問題