In WooCommerce I need to apply a custom handling fee for a specific payment gateway.
A custom handling fee for percentage cost and a custom handling for per fixed cost.
I have this 2 pieces of codes:
A) PERCENTAGE COST - function
/************************************************************/
/* 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
}
Result frontend
B) FIXED COST - function
/************************************************************/
/* 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;
}
Result frontend
The two distinct functions work perfectly separately. Now, I would like to combine the two functions to get the sum of the two costs.
My code attempt (A + B): function evolution with addition other payment systems
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
}
The function returns the following errors:
Notice: Undefined variable: surcharge in /MY-FOLDER..../snippet-ops.php(446) : eval()'d code on line 79
Notice: Undefined variable: fee in /MY FOLDER..../snippet-ops.php(446) : eval()'d code on line 79
That is exactly here:
$together = $surcharge + $fee;
Result frontend
Any advice on how to combine the 2 codes above into 1?