2017-12-23 9 views
1

クーポン機能を使用しない場合 'xyz'のような特定のお支払い方法IDに対して15%割引を適用したいと思います。WooCommerceで特定の選択されたお支払い方法の割引を追加してください

どのフックを使用するかを確認したいと思います。私が達成したいと思っていることの一般的な考え方は次のとおりです。

if payment_method_hook == 'xyz'{ 
    cart_subtotal = cart_subtotal - 15% 
} 

お客様はこのページで割引を表示する必要はありません。特定の支払い方法の場合にのみ、割引を適切に提出したいと思います。

答えて

0

woocommerce_cart_calculate_feesアクションフックにフックされたこのカスタム関数を使用すると、定義された支払方法に対して15%の割引を適用できます。

この機能では、実際の支払い方法ID (「bacs」、「cod」、「check」または「paypal」など)を設定する必要があります。

2番目の機能は、お支払い方法が選択されるたびにチェックアウトデータを更新します。

コード:

add_action('woocommerce_cart_calculate_fees','shipping_method_discount', 20, 1); 
function shipping_method_discount($cart_object) { 

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

    // HERE Define your targeted shipping method ID 
    $payment_method = 'bacs'; 

    // The percent to apply 
    $percent = 15; // 15% 

    $cart_total = $cart_object->subtotal_ex_tax; 
    $chosen_payment_method = WC()->session->get('chosen_payment_method'); 

    if($payment_method == $chosen_payment_method){ 
     $label_text = __("Shipping discount 15%"); 
     // Calculation 
     $discount = number_format(($cart_total/100) * $percent, 2); 
     // Add the discount 
     $cart_object->add_fee($label_text, -$discount, false); 
    } 
} 

add_action('woocommerce_review_order_before_payment', 'refresh_payment_methods'); 
function refresh_payment_methods(){ 
    // jQuery code 
    ?> 
    <script type="text/javascript"> 
     (function($){ 
      $('form.checkout').on('change', 'input[name^="payment_method"]', function() { 
       $('body').trigger('update_checkout'); 
      }); 
     })(jQuery); 
    </script> 
    <?php 
} 

コードは、あなたのアクティブな子のテーマ(またはアクティブテーマ)のfunction.phpファイルになります。

テスト済みで動作します。

+0

美しく、まさに私が求めていたものです。ロイックありがとう! –

関連する問題