【问题标题】:Add a custom fee for a specific payment gateway in Woocommerce在 Woocommerce 中为特定支付网关添加自定义费用
【发布时间】:2018-11-15 03:07:18
【问题描述】:

在选择付款方式(信用卡)时,如何在总金额中添加一个百分比?

例如:如果客户货到付款,那么底价,如果我选择在线支付,那么我收取的百分比加到总金额中?

货到付款的网关ID设置:cod 网上支付的网关ID设置:tinkoff

add_action( 'woocommerce_after_calculate_totals', 'custom_fee_for_tinkoff' );

function custom_fee_for_tinkoff( $cart ) {

 if ( is_checkout() || defined('WOOCOMMERCE_CHECKOUT') ) {

  $payment_method = WC()->session->get( 'chosen_payment_method' );

   if ($payment_method == 'tinkoff') {
    $percentage = 0.025;
    $surcharge = ( $cart->cart_contents_total + $cart->tax_total ) * $percentage;

    $cart->add_fee( 'Комиссия банка', $surcharge, true, '' );
  }
   else { return; }
 }
}

Image with problem

【问题讨论】:

    标签: php jquery wordpress woocommerce fee


    【解决方案1】:

    尝试以下重新访问的代码,它应该可以解决问题:

    // Add a custom fee based o cart subtotal
    add_action( 'woocommerce_cart_calculate_fees', 'custom_fee_for_tinkoff', 20, 1 );
    function custom_fee_for_tinkoff ( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
            return; // Only checkout page
    
        $payment_method = WC()->session->get( 'chosen_payment_method' );
    
        if ( 'tinkoff' == $payment_method ) {
            $surcharge = $cart->subtotal * 0.025;
            $cart->add_fee( 'Комиссия банка', $surcharge, true );
        }
    }
    
    // jQuery - Update checkout on methode payment change  
    add_action( 'wp_footer', 'custom_checkout_jqscript' );
    function custom_checkout_jqscript() {
        if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
            return; // Only checkout page
        ?>
        <script type="text/javascript">
        jQuery( function($){
            $('form.checkout').on('change', 'input[name="payment_method"]', function(){
                $(document.body).trigger('update_checkout');
            });
        });
        </script>
        <?php
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 谢谢!帮助并获得!上帝保佑你!
    猜你喜欢
    • 2020-06-01
    • 2018-09-24
    • 2017-03-02
    • 2017-08-05
    • 2014-06-06
    • 1970-01-01
    • 2019-07-25
    • 2017-04-04
    • 2023-03-03
    相关资源
    最近更新 更多