How to Define a percent surcharge by delivery country with WooCommerce

You are here: Home » Blog » CMS » WordPress » Plugins » WooCommerce » How to Define a percent surcharge by delivery country with WooCommerce

How to Define a percent surcharge by delivery country with WooCommerce

How to Define a percent surcharge by delivery country with WooCommerce: Similar to the 1% surcharge snippet, this will add a basic surcharge however only applied to specific countries. Keep in mind that you can reverse this and exclude from surcharge list with a simple little change
of

in_array

to

!in_array

and you are good to go.

add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
 global $woocommerce;
 if ( is_admin() && ! defined( 'DOING_AJAX' ) )
 return;
 $county = array('US');
 $percentage = 0.01;
 if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :
 $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
 $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
 endif;
}

( WordPress codex functions, hooks, in this snippet. )

add_actionis_admin,


Related Posts:

How to Add a percent surcharge to your cart / checkout with WooCommerce

How to Update: Automatically create media_buttons for shortcode selection

By |2019-02-08T19:25:18-04:00March 20th, 2018|Categories: CMS, Plugins, WooCommerce, WordPress|Tags: , , , |

About the Author:

I am a loving father, & husband. I am a computer enthusiast. I have used and enjoyed computers since I was young and I enjoy teaching young minds how to code, because it teaches them how to think. Today with YouTube, and social media garbage our youth are losing the ability to think on their own and solve problems. I believe this is a serious epidemic as kids today dont understand that technology is a tool. This tool is being abused, and its underlying effects are taking its toll on kids behaviour, and learning.