If you want to add an extra surcharge on all transactions on your WooCommerce store, then keep your close attention in this tutorial as I am going share how to add an extra surcharge to cart in WooCommerce store.
Change the $fee
to set the surcharge to a value as per your requirement. Below code will add an extra surcharge on all your transactions. Use the below code in your theme’s functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | add_action( 'woocommerce_cart_calculate_fees','wc_add_surcharge' ); function wc_add_surcharge() { global $woocommerce; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $county = array('US'); // change the $fee value as you want $fee = 1.00; if ( in_array( WC()->customer->get_shipping_country(), $county ) ) : $woocommerce->cart->add_fee( 'Surcharge', $fee, true, 'standard' ); endif; } |
Not want to add standard $
surcharge on all your transaction, then you can use the percentage surcharge on cart total amount. Copy the below code and add it to your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' ); function woocommerce_custom_surcharge() { global $woocommerce; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $percentage = 0.01; $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage; $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' ); } |
I hope this tutorial helps you, if you have any questions regarding WordPress, then feel free to put your comments in below comment section. Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool WordPress tutorials.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: woocommerce cart surcharge, woocommerce extra surcharge, woocommerce snip, woocommerce snippet, woocommerce snippets, woocommerce tutorial, woocommerce tutorials, wordpress woocommerce tutorial