I wrote This function to add a 9% extra fee on a gateway just on the checkout page.
Now, I want to exclude some products by ID from increasing additional fees in a function.
How can I simply do this with minimal changes in the code?
add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway' );
function add_checkout_fee_for_gateway() {
// Check if we are on the checkout page
if ( is_checkout() ) {
global $woocommerce;
$chosen_gateway = $woocommerce->session->chosen_payment_method;
if ( $chosen_gateway == 'paypal' ) {
$percentage = 0.09;
/* for all products prices + shipping (total price)
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
*/
$surcharge = ( $woocommerce->cart->get_subtotal() ) * $percentage;
$woocommerce->cart->add_fee( '9% value added tax', $surcharge, true, '' );
}
}
}
2
Answers
According to provided code, to implement functionality of excluding products, using product Id, from additional fees, the following code can be checked.
The code that you are using is a bit outdated since WooCommerce 3.
The following code will add a percentage fee from specific cart items calculated subtotal (excluding some defined products):
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.