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:

[code]in_array[/code]

change to:

[code]!in_array[/code]

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.

[php] /**
* 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;

}

[/php]

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?

 

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits,With a heart for tech and coding kits, in IT he never quits.At Magna's door, he took his stance, in Canada's wide expanse,At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance.With a passion deep for teaching code, to the young minds he showed,The path where digital seeds are sowed, in critical thinking mode.But alas, not all was bright and fair, at Magna's lair, oh despair,Harassment, intimidation, a chilling air, made the workplace hard to bear.Management's maze and morale's dip, made our hero's spirit flip,In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene,Yet in tech, his interest keen, continues to inspire and convene.