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

WooCommerce Snippet: Add Shipping Fee for Non-Continental States


/**
 * @snippet       Add Shipping Fee for Non-Continental States
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=19954
 * @author        Rodolfo Melogli
 * @compatible    WC 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_cart_calculate_fees', 'bbloomer_add_cart_fee' );

function bbloomer_add_cart_fee() {
$noncontinental = array('AK','HI');
if( in_array( WC()->customer->shipping_state, $noncontinental ) ) {
 $surcharge = 0.05 * WC()->cart->shipping_total; // 5% surcharge based on shipping cost
 WC()->cart->add_fee( __('Non-continental Shipping', 'woocommerce'), $surcharge );
}
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

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

  1. 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

  2. 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?

  3. 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

    2. 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? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.