在Woo商务中,我需要对特定的支付网关应用自定义处理费。
百分比成本的自定义处理费和每个固定成本的自定义处理费。
我有这2段代码:
A) 成本百分比 - 函数
/************************************************************/
/* PERCENTACE COST
**/
// Add a custom fee based o cart subtotal
// Add a custom fee based o cart subtotal
add_action( 'woocommerce_cart_calculate_fees', 'custom_percentage_fee', 20, 1 );
function custom_percentage_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return; // Only checkout page
$payment_method = WC()->session->get( 'chosen_payment_method' );
if ( 'cod' == $payment_method ) {
$surcharge = $cart->subtotal * 0.025;
$cart->add_fee( 'Percentage Cost', $surcharge, true );
}
}
// jQuery - Update checkout on methode payment change
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) )
return; // Only checkout page
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
}
结果前端
B) 固定成本 - 功能
/************************************************************/
/* FIXED COST
**/
// Add a custom fee based o cart subtotal
add_action( 'woocommerce_cart_calculate_fees', 'custom_fixed_fee', 10, 1 );
function custom_fixed_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( 'cod' === WC()->session->get('chosen_payment_method') ) {
$fee = 0.31;
$cart->add_fee( 'Fixed Cost', $fee, true );
}
}
开满天机