Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wbcomdev/a51c9bf3cfadffbaefaef53d616191af to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/a51c9bf3cfadffbaefaef53d616191af to your computer and use it in GitHub Desktop.
/**
* 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.
*/
function wb_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;
}
add_action( 'woocommerce_cart_calculate_fees', 'wb_woocommerce_custom_surcharge' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment