在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' );
}
此功能会向所有交易添加费用。
是否可以调整此功能,使其仅适用于特定付款方式?
另一个问题是我希望将此费用应用到购物车上。是否有可能?
我也欢迎任何其他替代方法。我知道类似的“ Payment Gateway Based Fees” woo插件,但是我买不起。
2020更新
注意:所有付款方式仅在“结帐”页面上可用。
以下代码将根据所选的付款方式有条件地增加特定费用:
// 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( '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文件中。测试和工作。
如何在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文件中。使用后,将其删除。
类似答案:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句