4

在 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 插件,但我买不起。

4

4 回答 4

23

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 结帐页面中找到特定的付款方式 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 文件中。使用后,将其取下。

在此处输入图像描述


类似的答案:

于 2016-07-14T21:21:45.890 回答
1

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

  1. 我们将使用唯一的过滤器钩子woocommerce_cart_calculate_fees
  2. 为了获得用户选择的付款方式,我们必须使用此方法从用户会话中检索它WC()->session->get( 'chosen_payment_method' )
  3. calculate_fees()calculate_totals()方法不是必需的。
  4. 我们将使用 cart 方法添加费用,该方法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

于 2021-11-22T17:08:51.917 回答
0

对于其他想要这样做的人,我想为银行转账 (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);
}
于 2017-06-01T21:41:41.410 回答
0
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, '' );
    }


}
于 2018-08-14T07:59:29.220 回答