Ecommerce Shopify WordPress Discussion

Exclude some products from calculated additional fee in WooCommerce

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, '' ); } } }
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): add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway', 10, 1 ); function add_checkout_fee_for_gateway( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Only on checkout page and for specific payment method ID if ( is_checkout() && ! is_wc_endpoint_url() && WC()->session->get('chosen_payment_method') === 'paypal' ) { // Here below define the product IDs to be excluded $excluded_product_ids = array(15, 18); $percentage_rate = 0.09; // Defined percentage rate $custom_subtotal = 0; // Initializing // Loop through cart items foreach( $cart->get_cart() as $item ) { // Calculate items subtotal from non excluded products if( ! in_array($item['product_id'], $excluded_product_ids) ) { $custom_subtotal += (float) $item['line_subtotal']; } } if ( $custom_subtotal > 0 ) { $cart->add_fee( __('9% value added tax'), ($custom_subtotal * $percentage_rate), true, '' ); } } } Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

January 7, 2024

Thank you Mr. @loictheaztec . I Tested your code it works well. I need a message like this '9% value added tax' If the basket (order) has excluded_product . like Message above just a message additional when only the order has excluded products. example below 'Some products are tax free' That is, if the order included exception products, instead of a message. Two messages will be displayed. The second message will be shown only if the exception products are in the basket (in the order). I wrote function but I think bad way and not clean code! help from ai! Made many changes to your code. Your code is clean and good. I didn't want that to happen. I'm not even sure if the code works! add_action( 'woocommerce_cart_calculate_fees', 'add_checkout_fee_for_gateway', 10, 1 ); function add_checkout_fee_for_gateway( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // Only on checkout page and for specific payment method ID if ( is_checkout() && ! is_wc_endpoint_url() && WC()->session->get('chosen_payment_method') === 'paypal' ) { // Here below define the product IDs to be excluded $excluded_product_ids = array(15, 18); $percentage_rate = 0.09; // Defined percentage rate $custom_subtotal = 0; // Initializing $contains_excluded_products = false; // Initializing // Loop through cart items foreach( $cart->get_cart() as $item ) { // Calculate items subtotal from non-excluded products if( ! in_array($item['product_id'], $excluded_product_ids) ) { $custom_subtotal += (float) $item['line_subtotal']; } else { $contains_excluded_products = true; } } if ( $custom_subtotal > 0 ) { $cart->add_fee( __('9% value added tax'), ($custom_subtotal * $percentage_rate), true, '' ); } if ( $contains_excluded_products ) { $cart->add_fee( __('Some products are tax free'), 0, false, '' ); } } } How can I simply do this with minimal changes in your code? Please do not delete or edit your previous answer. Let it stay there. Because some may need it too. If you can, please write a new answer for this part of my new question so that if we want to implement such a scenario, we can do it. Do you approve of this change by artificial intelligence? Is it a good way? Is this code good in general? Or it can be done better than that. And can it be written with other commands and a better code? can you help? Thank you again a lot.

January 7, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.