Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active August 14, 2017 00:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChromeOrange/6638580 to your computer and use it in GitHub Desktop.
Save ChromeOrange/6638580 to your computer and use it in GitHub Desktop.
Add a surcharge based on delivery country
/**
* 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_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;
}
@ChromeOrange
Copy link
Author

Corrected to remove 'standard' from add_fee based on woocommerce/woocommerce#6808

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