WooCommerce: Add Shipping Fee for Non-Continental States

Today we take a look at the WooCommerce Checkout page and specifically at how to add a Custom Extra Fee for Non-Continental US States.

You can do the exact same thing for a non-US State, as long as customers will feel comfortable in paying extra!

WooCommerce: Add Shipping Surcharge for Specific States
WooCommerce: Add Shipping Surcharge for Specific States

PHP Snippet: Add Shipping Fee for Non-Continental States @ WooCommerce Checkout

/**
 * @snippet       Add Shipping Fee for Non-Continental States
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_cart_fee_for_states' );

function bbloomer_add_cart_fee_for_states() {
   $noncontinental = array( 'AK', 'HI' ); // ARRAY OF STATE CODES
   if ( in_array( WC()->customer->get_shipping_state(), $noncontinental ) ) {
      $surcharge = 0.05 * WC()->cart->shipping_total; // 5% surcharge based on shipping cost
      WC()->cart->add_fee( 'Non-continental Shipping', $surcharge );
   }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]
  • WooCommerce: Hide Shipping Rates if Free Shipping Available
    If Free Shipping is available, you possibly don’t want to show the other premium shipping options. WooCommerce shows by default all shipping rates that match a given shipping zone, so it’s not possible to achieve this from the settings alone. Thankfully, the “woocommerce_package_rates” filter allows us to manipulate the shipping rates before they are returned […]
  • WooCommerce: Add Checkout Fee for a Payment Gateway (e.g. PayPal)
    Here’s a simple PHP snippet to add a fee to the checkout for every payment or for a specific payment gateway. Please do remember that for certain payment gateways such as PayPal, adding checkout fees is currently against their Terms of Service so make sure to check this first. As usual, this needs to be […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

11 thoughts on “WooCommerce: Add Shipping Fee for Non-Continental States

  1. I am getting a php warning when using this snippet:

    php 8.0, WC 6.8

    PHP message: shipping_state was called incorrectly. Customer properties should not be accessed directly.

    1. You’re right! I’ve just updated the snippet, let me know

  2. Our courier has a variable fuel surcharge that changes monthly – I wanted to find an easy way to add it to the shipping total – this works but shows as a separate line item – is there any way to add this to the shipping total instead and just show one shipping fee? Otherwise I am going to have lots of complaints etc.

    1. Hello Cat, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi Rodolfo
    Nice snippet,,,
    Wondering if I modified it to check for specific postcodes it would work?
    Also for logged in users as well?
    Maybe the postcode wildcard * will stop it functioning?
    And the $surcharge = 5 +,,, would just add 5 to the total?

    I am running a live site so asking before I implement anything ๐Ÿ™‚

    function bbloomer_add_cart_fee() {
    global $woocommerce;
    $isleofwight = array('PO30*','PO31*','PO32*','PO33*','PO34*','PO35*','PO36*','PO37*','PO38*','PO39*','PO40*','PO41*');
    if( in_array( WC()->customer->shipping_postcode, $isleofwight ) ) {
     $surcharge = 5 + WC()->cart->shipping_total; // 5% surcharge based on shipping cost
     $woocommerce->cart->add_fee( __('Isle of Wight Shipping', 'woocommerce'), $surcharge );
    }
    }
     
    add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_cart_fee' );
    

    Cheers

    1. Andy, thanks so much for your comment! Unfortunately this is custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

  4. Hi!
    This is a great snippet. It works really well on cart page. But when I go to checkout page and change the address this code does not work. Is there any solution for that?

    Thank you in advance,
    Priya Jolly

    1. Hey Priya, thanks for your comment! When you changed the address, what did you change that to?

  5. Rodolfo,
    Not sure how old this post is and whether you are still taking comments. Once this PHP is added to functions, what else needs to be done to get it to show up? I am not seeing this show up on my shopping cart checkout. Of course I am completely making up a zip code in Alaska, perhaps that is why?

    1. Liz, thanks for your comment ๐Ÿ™‚ And yes – I always take comments – I love it! Give me a minute and I’ll test this on the latest WooCommerce version, they changed a lot re: shipping in 2.6

      1. Yes, it definitely works for US/AL and US/HI. It will work out of the box and no matter the zip code you enter ๐Ÿ™‚

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *