根据 WooCommerce 中的特定付款方式添加费用
Add fee based on specific payment methods in WooCommerce
在 WooCommerce 中,我需要为特定的支付网关申请自定义手续费。我从这里得到了这段代码: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' );
}
此功能为所有交易添加费用。
是否可以调整此功能并使其仅适用于特定的付款方式?
另一个问题是我希望将此费用应用于购物车。可能吗?
我也欢迎任何替代方法。我知道类似的“基于支付网关的费用”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 文件。已测试并有效。
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;
}
Code goes in functions.php file of your active child theme (or active theme). Once used, remove it.
相似答案:
对于其他想要这样做的人,我想为银行转账 (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, '' );
}
}
首先我们需要了解一些关键的事情:
- 我们将使用唯一的过滤器钩子
woocommerce_cart_calculate_fees
- 为了获得用户选择的付款方式,我们必须使用此方法从用户会话中检索它
WC()->session->get( 'chosen_payment_method' )
calculate_fees()
和 calculate_totals()
方法不是必需的。
- 我们将使用接受两个参数的购物车方法
WC()->cart->add_fee()
添加费用 – 第一个是费用说明,第二个是费用金额。
- 还有一件事 – 我们需要一个付款方式 slug,这是本教程中描述的最简单的获取方式 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
在 WooCommerce 中,我需要为特定的支付网关申请自定义手续费。我从这里得到了这段代码: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' );
}
此功能为所有交易添加费用。
是否可以调整此功能并使其仅适用于特定的付款方式?
另一个问题是我希望将此费用应用于购物车。可能吗?
我也欢迎任何替代方法。我知道类似的“基于支付网关的费用”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 文件。已测试并有效。
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; }
Code goes in functions.php file of your active child theme (or active theme). Once used, remove it.
相似答案:
对于其他想要这样做的人,我想为银行转账 (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, '' );
}
}
首先我们需要了解一些关键的事情:
- 我们将使用唯一的过滤器钩子
woocommerce_cart_calculate_fees
- 为了获得用户选择的付款方式,我们必须使用此方法从用户会话中检索它
WC()->session->get( 'chosen_payment_method' )
calculate_fees()
和calculate_totals()
方法不是必需的。- 我们将使用接受两个参数的购物车方法
WC()->cart->add_fee()
添加费用 – 第一个是费用说明,第二个是费用金额。 - 还有一件事 – 我们需要一个付款方式 slug,这是本教程中描述的最简单的获取方式 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