Home / functions.php / Define a % surcharge by delivery country with WooCommerce
  • How to Define a % surcharge by delivery country with WooCommerce

    define-a-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 in_array to !in_array and you are good to go. We have the wonderful people at WooCommerce to thank for this snippet you can view the original here.

    ( click code to copy )

    wordpress snippet : 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;
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    add_action, is_admin,