根据WooCommerce中的特定付款方式添加费用 [英] Add fee based on specific payment methods in WooCommerce

查看:404
本文介绍了根据WooCommerce中的特定付款方式添加费用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WooCommerce中,我需要为特定的支付网关支付自定义手续费.我从这里获得了这段代码:如何向WooCommerce Checkout添加手续费.

In WooCommerce I need to apply a custom handling fee for a specific payment gateway. I have this piece of code from here: How to Add Handling Fee to WooCommerce Checkout.

这是我的代码:

add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
    global $woocommerce;

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

        $fee = 5.00;
    $woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );
}

此功能会向所有交易收取费用.

This function add a fee to all transactions.

是否可以调整此功能,使其仅适用于特定付款方式?

Is it possible to tweak this function and make it apply for specific payment method only ?

另一个问题是我希望将此费用应用到购物车上.有可能吗?

The other problem is that I want this fee to be applied on cart. Is it possible?

我也欢迎任何其他替代方法.我知道类似的基于付款网关的费用" woo插件,但我买不起.

I will welcome any alternative method as well. I know about the similar "Payment Gateway Based Fees" woo plugin, but I can't afford it.

推荐答案

2020更新

注意:所有付款方式仅在结帐"页面上可用.

Note: All payment methods are only available on Checkout page.

以下代码将根据所选的付款方式有条件地收取特定费用:

The following code will add conditionally a specific fee based on the chosen payment method:

// Add a custom fee (fixed or based cart subtotal percentage) by payment
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee' );
function custom_handling_fee ( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

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

    if ( empty( $chosen_payment_id ) )
        return;

    $subtotal = $cart->subtotal;

    // SETTINGS: Here set in the array the (payment Id) / (fee cost) pairs
    $targeted_payment_ids = array(
        'cod' => 8, // Fixed fee
        'paypal' => 5 * $subtotal / 100, // Percentage fee
    );

    // Loop through defined payment Ids array
    foreach ( $targeted_payment_ids as $payment_id => $fee_cost ) {
        if ( $chosen_payment_id === $payment_id ) {
            $cart->add_fee( __('Handling fee', 'woocommerce'), $fee_cost, true );
        }
    }
}

您需要执行以下操作以刷新付款方式更改后的结帐,以使其生效:

You will need the following to refresh checkout on payment method change, to get it work:

// jQuery - Update checkout on payment method change
add_action( 'wp_footer', 'custom_checkout_jquery_script' );
function custom_checkout_jquery_script() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('form.checkout').on('change', 'input[name="payment_method"]', function(){
            $(document.body).trigger('update_checkout');
        });
    });
    </script>
    <?php
    endif;
}

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

Code goes in functions.php file of your active child theme (or active theme). tested and works.

如何在WooCommerce Checkout页面中找到特定的付款方式ID?

以下将在结帐付款方式上显示仅适用于管理员的付款ID:

How to find a specific payment method ID in WooCommerce Checkout page?

The following will display on checkout payment methods the payment Id just for admins:

add_filter( 'woocommerce_gateway_title', 'display_payment_method_id_for_admins_on_checkout', 100, 2 );
function display_payment_method_id_for_admins_on_checkout( $title, $payment_id ){
  if( is_checkout() && ( current_user_can( 'administrator') || current_user_can( 'shop_manager') ) ) {
      $title .= ' <code style="border:solid 1px #ccc;padding:2px 5px;color:red;">' . $payment_id . '</code>';
  }
  return $title;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.使用后,将其删除.

Code goes in functions.php file of your active child theme (or active theme). Once used, remove it.


类似的答案:


Similar answer:

  • Add a custom fee for a specific payment gateway in Woocommerce
  • Add a fee based on shipping method and payment method in Woocommerce
  • Percentage discount based on user role and payment method in Woocommerce

这篇关于根据WooCommerce中的特定付款方式添加费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆