在 WooCommerce 中,我需要为特定支付网关申请自定义手续费。
按百分比成本收取的自定义手续费和按固定成本收取的自定义手续费。
我有这个2 pieces代码:
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 );
}
}
// 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() ) :
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}
结果前端
这两个不同的功能可以完美地分开工作。现在,我想结合这两个函数来获得两个成本的总和。
我的代码尝试(A + B):与其他支付系统的功能演变
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 ( 'stripe' == $payment_method ) {
$surcharge = $cart->subtotal * 0.025;
$fee = 0.31;
}
if ( 'paypal_credit_card_rest' == $payment_method ) {
$surcharge = $cart->subtotal * 0.036;
$fee = 0.35;
}
if ( 'paypal_express' == $payment_method ) {
$surcharge = $cart->subtotal * 0.036;
$fee = 0.35;
}
if ( 'bacs' == $payment_method ) {
return;
}
$together = $surcharge + $fee;
$cart->add_fee( 'Plus Cost', $together, 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
}
该函数返回以下错误:
注意:未定义的变量:/MY-FOLDER..../snippet-ops.php(446) 中的附加费:第 79 行的 eval() 代码
注意:未定义的变量:/MY FOLDER..../snippet-ops.php(446) 中的费用:第 79 行的 eval() 代码
那正是在这里:
$together = $surcharge + $fee;
结果前端
关于如何将上面的 2 个代码合并为 1 个有什么建议吗?