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!
PHP Snippet: Add Shipping Fee for Non-Continental States @ WooCommerce Checkout
/**
* @snippet Add Shipping Fee for Non-Continental States
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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 );
}
}
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.
You’re right! I’ve just updated the snippet, let me know
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.
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!
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 🙂
Cheers
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
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
Hey Priya, thanks for your comment! When you changed the address, what did you change that to?
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?
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
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 🙂