2017-04-10 2 views
1

私はここに来てthis question hereから私のWooCommerceサイトの解決策を探していました。私はカート内のアイテムの数が偶数の場合にのみカート内の割引を得る方法を探しています。例えばカートのすべてのNTH商品のカスタム割引

カートに1つだけのアイテムがある場合:全価格はカートで
2アイテム: - (マイナス)はカート内
5項目に両方のための
2 $: - (マイナス)、それの4のための 2 $(カート内のアイテムの数が奇数かどう1つの項目は常に完全な価格を持っています。)ところで

、私の製品のすべてが同じを持っています価格。

誰かが私が言及した質問リンクの男のために助けたように誰かが私を助けることができるだろうか?ここで

答えて

2

は、あなたが期待している何をしますwoocommerce_cart_calculate_feesアクションフックに引っかけカスタム関数です:

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

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

    $cart_count = 0; 

    foreach($cart_object->get_cart() as $cart_item){ 
     // Adds the quantity of each item to the count 
     $cart_count += $cart_item["quantity"]; 
    } 

    // For 0 or 1 item 
    if($cart_count < 2) { 
     return; 
    } 
    // More than 1 
    else { 
     // Discount calculations 
     $modulo = $cart_count % 2; 
     $discount = (-$cart_count + $modulo); 

     // Adding the fee 
     $discount_text = __('Discount', 'woocommerce'); 
     $cart_object->add_fee($discount_text, $discount, false); 
    } 
} 

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

このコードはテスト済みであり、動作します。

+0

$ discount =( - $ cart_count + $ modulo)* 2; '... – Reigel

+0

@Reigel質問はあまり明確ではありません...明らかにOPはプログレッシブディスカウント2 $ each 2 items ...私のコードはこのケースで動作します。 – LoicTheAztec

+0

はい、それは明らかではないと同意した。 – Reigel

関連する問題