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

change to:

!in_array

and you are good to go. The exclamation mark (!) in front of in_array means NOT in array. (The country that is.)  🙂

Now, How to Define a percent surcharge by delivery country with WooCommerce? Add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme. On Line 12: change ‘US‘ to what ever country code you would like.

/**
 * Add a 1% surcharge to your cart / checkout based on delivery country
 * Taxes, shipping costs and order subtotal are all included in the surcharge amount
 */
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;
 
}

The above code will add a surcharge to the defined $county which is in this case ‘US’. Lets say you wanted to charge all other countries except the ‘US’?

On line 15 replace if ( in_array( $woocommerce….

Just add the exclamation mark like this: if ( !in_array( $woocommerce….

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

add_actionis_admin,


Related Videos:

 


Related Posts:

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

How to Update: Automatically create media_buttons for shortcode selection

Create a PayPal Donation Shortcode – WordPress

Using the Current Year in Your Posts – WordPress

Are there copyright restrictions on the use of Python?

CODING WITH CSS: The style attribute

Change the Author Permalink Structure

How To Start a Blog – Beginner’s Guide for 2020

The background-color CSS property

Avada Theme Version 5.5.2

What is the use of New Keyword in VB.NET?