繁体   English   中英

根据 WooCommerce 中的特定付款方式添加费用

[英]Add fee based on specific payment methods in WooCommerce

在 WooCommerce 中,我需要为特定的支付网关收取自定义手续费。 我从这里有这段代码: 如何向 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' );
}

此功能向所有交易添加费用。

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

另一个问题是我希望将此费用应用于购物车。 是否可以?

我也欢迎任何替代方法。 我知道类似的“基于支付网关的费用”woo 插件,但我买不起。

2021 更新

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

以下代码将根据选择的付款方式有条件地添加特定费用:

// 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 );
        }
    }
}

您将需要以下内容来刷新付款方式更改的结帐,以使其正常工作:

// jQuery - Update checkout on payment method change
add_action( 'woocommerce_checkout_init', 'payment_methods_refresh_checkout' );
function payment_methods_refresh_checkout() {
    wc_enqueue_js( "jQuery( function($){
        $('form.checkout').on('change', 'input[name=payment_method]', function(){
            $(document.body).trigger('update_checkout');
        });
    });");
}

代码位于活动子主题(或活动主题)的 functions.php 文件中。 测试和工作。

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

以下内容将在结账付款方式中显示仅供管理员使用的付款 ID:

 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 文件中。 使用后,将其删除。

在此处输入图片说明


类似答案:

对于其他想要这样做的人,我想为银行转账 (BACS) 添加费用,这是我使用的方法:

//Hook the order creation since it is called during the checkout process:
add_filter('woocommerce_create_order', 'my_handle_bacs', 10, 2);

function my_handle_bacs($order_id, $checkout){
//Get the payment method from the $_POST
    $payment_method = isset( $_POST['payment_method'] ) ? wc_clean( $_POST['payment_method'] ) : '';

//Make sure it's the right payment method
    if($payment_method == "bacs"){

        //Use the cart API to add recalculate fees and totals, and hook the action to add our fee
        add_action('woocommerce_cart_calculate_fees', 'my_add_bacs_fee');
        WC()->cart->calculate_fees();
        WC()->cart->calculate_totals();
    }

    //This filter is for creating your own orders, we don't want to do that so return the $order_id untouched
    return $order_id;
}
function my_add_bacs_fee($cart){
    //Add the appropriate fee to the cart
    $cart->add_fee("Bank Transfer Fee", 40);
}
add_action( 'woocommerce_cart_calculate_fees','cod_fee' );
function cod_fee() {
    global $woocommerce;

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
        // get your payment method
        $chosen_gateway = WC()->session->chosen_payment_method;
        //echo $chosen_gateway;
        $fee = 5;
        if ( $chosen_gateway == 'cod' ) { //test with cash on delivery method
        WC()->cart->add_fee( 'delivery fee', $fee, false, '' );
    }


}

首先,我们需要了解一些关键的事情:

  1. 我们将使用唯一的过滤器钩子,即woocommerce_cart_calculate_fees
  2. 为了获得用户选择的付款方式,我们必须使用此方法从用户会话中检索它WC()->session->get( 'chosen_payment_method' )
  3. 不需要calculate_fees()calculate_totals()方法。
  4. 我们将使用接受两个参数的购物车方法WC()->cart->add_fee()添加费用 - 第一个是费用描述,第二个 - 费用金额。
  5. 还有一件事——我们需要一个支付方式,这是本教程中描述的最简单的方法https://rudrastyh.com/woocommerce/add-id-column-to-payment-methods-table.html

现在走吧:

add_action( 'woocommerce_cart_calculate_fees', 'rudr_paypal_fee', 25 );
function rudr_paypal_fee() {

    if( 'paypal' == WC()->session->get( 'chosen_payment_method' ) ) {
        WC()->cart->add_fee( 'PayPal fee', 2 ); // let's add a fee for paypal
    }

}

每次更改支付网关时刷新结帐也很棒,使用以下代码很容易做到:

jQuery( function( $ ) {
    $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
        $( 'body' ).trigger( 'update_checkout' );
    });
});

就是这样。 这里也详细描述了一切: https : //rudrastyh.com/woocommerce/charge-additional-fees-based-on-payment-gateway.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM