Skip to content

Instantly share code, notes, and snippets.

@nielslange
Last active June 17, 2022 11:04
Show Gist options
  • Save nielslange/47c3a4f6fc0d9efa672922ac1b3e7fd2 to your computer and use it in GitHub Desktop.
Save nielslange/47c3a4f6fc0d9efa672922ac1b3e7fd2 to your computer and use it in GitHub Desktop.
WooCommerce: Add a surcharge based on the delivery country
<?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
*
* Change $percentage to set the surcharge to a value to suit
*
* Add countries to array('US'); to include more countries to surcharge
* http://en.wikipedia.org/wiki/ISO_3166-1#Current_codes for available alpha-2 country codes
*
* Change in_array to !in_array to EXCLUDE the $countries array from surcharges
*
* Uses the WooCommerce fees API
* Add to theme functions.php
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_country_surcharge' );
function woocommerce_custom_country_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$percentage = 0.01;
$country = array( 'US' );
if ( in_array( $woocommerce->customer->get_shipping_country(), $country ) ) {
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
if ( $woocommerce->customer->get_shipping_state() == 'CO' ) ) {
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );
}
}
}
<?php
/**
* Add a 1% surcharge to your cart / checkout based on delivery state
* Taxes, shipping costs and order subtotal are all included in the surcharge amount
*
* Change $percentage to set the surcharge to a value to suit
*
* Change $state to set the state
* Example: The state code for Colorado would be 'CO'
* https://en.wikipedia.org/wiki/ISO_3166-2:US
*
* Uses the WooCommerce fees API
* Add to theme functions.php
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_state_surcharge' );
function woocommerce_custom_state_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$percentage = 0.01;
$state = 'CO';
if ( $woocommerce->customer->get_shipping_state() == $state ) ) {
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );
}
}
@rplakas
Copy link

rplakas commented Aug 16, 2018

Thanks for this snippet!

$county or $country ?

If I want to add a surcharge to all countries except the US would it be;

if ( !in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :

@nielslange
Copy link
Author

Thanks for this snippet!

$county or $country ?

If I want to add a surcharge to all countries except the US would it be;

if ( !in_array( $woocommerce->customer->get_shipping_country(), $county ) ) :

Sorry for my late reply, @rplakas. I totally missed your comment and stumbled upon it. I corrected the typo and your code to exclude a country is correct. if ( ! in_array( $woocommerce->customer->get_shipping_country(), $county ) ) { ... } is the way to exclude a country.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment