It doesn’t make sense to create a whole new shipping zone or rate table when you just want to tweak the delivery price to a small number of postcodes. Fortunately, WooCommerce makes it easy to apply surcharges with just a couple of lines of code.
//Add an extra delivery fee for certain postcodes
add_action( 'woocommerce_cart_calculate_fees', 'launchwoo_specific_postcode_add_cart_fee' );
function launchwoo_specific_postcode_add_cart_fee() {
//We only want to apply this if Local Pickup is not the shipping method
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if (( 0 === strpos( $chosen_shipping, 'local_pickup' ) ) == false) {
//Comma separated list of postcodes to surcharge
$outermetro = array('5250','5251', '5355', '5171', '5351', '5118', '5152', '5051', '5173');
//Apply the surcharge if it's an outer metro postcode
if ( in_array( WC()->customer->get_shipping_postcode(), $outermetro ) ) {
$surcharge = 3; //Set the surcharge amount in dollars
WC()->cart->add_fee( __('Outer Metro Delivery', 'woocommerce'), $surcharge );
}
}
}
Installing the code
Just copy and paste the snippet to your theme’s functions.php file or to a MU Plugin. You can also use the Code Snippets plugin to install the code, which is handy if you don’t have access to the filesystem.
Also worth noting—
If your store offers digital downloads of products, or some other means of fulfilling orders that doesn’t incur shipping costs, you’ll need to exclude them from the surcharge in the same way we excluded local_pickup.
Finally, the surcharge won’t be quietly added to the shipping cost. Instead, it will appear as a standalone line item on the checkout page. If you want to increase the cost of shipping for certain postcodes without disclosing that to customers, you’ll probably need to create a new Shipping Zone in WooCommerce’s settings.
Hi Shane. I had been searching for this solution for quiet and tested a few plugins. Your code worked wonders. Way easier and better than all the plugins i tested, plus its less bloat as I dont even need the plugin. Thanks for the Help!!!!!!!
This is working great for us to, thanks 🙂 One of our couriers annoyingly surcharges by Suburb, so not necessarily the entire postcode. I have been trying to alter the above code to add a surcharge based on a list of Suburbs instead but can’t quite get it working. Any help would be hugely appreciated.
Unfortunately there’s no suburb equivalent of get_shipping_postcode. Perhaps a plugin like ‘Shipping per Neighborhood for WooCommerce’ could be the answer?
This is really cool and it works. However for UK postcodes that a ‘space’ would it still work?