2017-09-17 11 views
1

特定の変数製品に特定の割引を設定したい場合、ある製品を購入して50%割引でもう1つ買うと%off)。私は多くの割引のプラグインは、私が発見したその最も近いを購入しようとしましたです:WooCommerceWooCommerce割引:1つ購入すると50%OFF

ため

  • 価格割引

  • WooCommerceすべての割引Liteの

  • WooCommerce拡張クーポンの特長

これらのプラグインを使用することで、私は各製品の小計または割引の割引を設定することができましたが、私が探しているものではありません。私はそれのために行きたくない他のプロプラグインがあります。

プラグインを購入せずに達成できますか?

おかげで見つかり

何か似て https://www.fldtrace.com/buy-3-get-1-free-coupon-woocommerce

+0

各製品のWooCommerceディスカウントカートについて確認するこのhttps://www.cloudways.com/blog/woocommerce-discount-cart/をご確認ください。 –

答えて

2

更新(あなたのコメントに関連する)

このバージョンでは、定義され、このために、カート内のすべての製品バリエーションでグローバルに動作します可変プロダクト:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1); 
function add_custom_discount_2nd_at_50($wc_cart){ 
    if (is_admin() && ! defined('DOING_AJAX')) return; 
    $discount = 0; 
    $items_prices = array(); 

    // Set HERE your targeted variable product ID 
    $targeted_product_id = 40; 

    foreach ($wc_cart->get_cart() as $key => $cart_item) { 
     if($cart_item['product_id'] == $targeted_product_id){ 
      $qty = intval($cart_item['quantity']); 
      for($i = 0; $i < $qty; $i++) 
       $items_prices[] = floatval($cart_item['data']->get_price()); 
     } 
    } 
    $count_items_prices = count($items_prices); 
    if($count_items_prices > 1) foreach($items_prices as $key => $price) 
     if($key % 2 == 1) $discount -= number_format($price/2, 2); 

    if($discount != 0){ 
     // Displaying a custom notice (optional) 
     wc_clear_notices(); 
     wc_add_notice(__("You get 50% of discount on the 2nd item"), 'notice'); 

     // The discount 
     $wc_cart->add_fee('Discount 2nd at 50%', $discount, true ); 
     # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 
    } 
} 

コードは、アクティブな子テーマ(またはテーマ)のfunction.phpファイル、またはすべてのプラグインファイルに入ります。

このコードはWoocommerce 3+でテストされ、動作します。


オリジナルの答え:

特定の変数の製品IDのために第二項目の50%のカスタム割引を追加行うには、多くの方法があります。私はadd_fee()メソッドを負の値で使用しています(したがって、割引を追加します)
必要に応じて、それはカスタム通知が表示されます。

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1); 
function add_custom_discount_2nd_at_50($wc_cart){ 
    if (is_admin() && ! defined('DOING_AJAX')) return; 
    $discount = 0; 

    // Set HERE your targeted variable product ID 
    $targeted_product_id = 40; 

    foreach ($wc_cart->get_cart() as $key => $cart_item) { 
     if($cart_item['product_id'] == $targeted_product_id){ 
      $price = $cart_item['data']->get_price(); 
      $quantity = intval($cart_item['quantity']); 
      for($i = 1, $j = 0; $i <= $quantity; $i++){ 
       if($i % 2 == 0 && $quantity > 1) $j++; 
      } 
      if($quantity > 1) number_format($discount -= $price * $j/2, 2); 
     } 
    } 
    if($discount != 0){ 
     // Displaying a custom notice (optional) 
     wc_clear_notices(); 
     wc_add_notice(__("You get 50% of discount on the 2nd item"), 'notice'); 

     $wc_cart->add_fee('Discount 2nd at 50%', $discount, true ); 
     # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 
    } 
} 

コードは、任意のプラグインファイルでも、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルになりますか。

このコードはWoocommerce 3+でテストされ、動作します。

+0

help @loictheaztecのおかげで、私はコードを試してみました。同じバリエーションを持つ同じ製品の1つ以上の量を追加しています。しかし、私は同じ製品の異なるバリエーションを追加しようとしているとき、それは動作していません。 **ここには** https://ibb.co/k7CtzQ https://ibb.co/j1v2tk – abdul

+0

@abdulに役立つスクリーンショットがあります。複数のバリエーションでこれを作成するには、すべてのバリエーションで同じ価格が。だから価格が同じなら私はそれをすることができます。 – LoicTheAztec

+0

さまざまな価格のバリエーションの提案?そうでなければ、私は価格を調整し、同じ価格で行かなければならないと思う。 – abdul

関連する問題