Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active July 5, 2023 22:15
Show Gist options
  • Save woogists/cbd29812339d7cac083896e0c30cb8de to your computer and use it in GitHub Desktop.
Save woogists/cbd29812339d7cac083896e0c30cb8de to your computer and use it in GitHub Desktop.
[Frontend Snippets][Add a surcharge to cart and checkout] Add a surcharge based on the 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
*/
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;
}
@aquaextensions
Copy link

Good morning @woogists,

I'm currently using this code with an additional plugin (pricebasedcountry.com) to show Canadian users the CAD $ pricing and then convert back to USD with a -0.25 percentage. I know that the tax is calculated on the Subtotal of the items in the cart, but I was wondering if there's a way to apply (or modify) this code to have the original tax shown on the Cart & Checkout Page?

My main goal is to show everything in CAD $ pricing on the Cart/Checkout review section and then have the USD $ pricing pushed shown as the Final Total (and sent to Authorize.net). See the attached screenshot.

I thought maybe there might be a way to re-add the 'discounted' percentage back into the Tax, but I'm not sure if WooCommerce has a hook to manipulate the tax calculation.

CAD   USD Review Checkout

Any help would be much appreciated!

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