Remote Area Shipping Surcharge
Adds a surcharge to the order when the shipping postcode is in a remote area you list, such as islands or highland regions that carriers charge extra for.
Opens WPIDE on
Code
$wpide_remote = array(
'prefixes' => array( 'HS', 'ZE', 'KW15', 'PA41' ), // Postcode prefixes.
'amount' => 9.95, // Surcharge.
'label' => 'Remote area surcharge',
'taxable' => true,
);
add_action( 'woocommerce_cart_calculate_fees', function ( $cart ) use ( $wpide_remote ) {
if ( ( is_admin() && ! wp_doing_ajax() ) || ! $cart->needs_shipping() ) {
return;
}
$postcode = strtoupper( str_replace( ' ', '', (string) WC()->customer->get_shipping_postcode() ) );
if ( '' === $postcode ) {
return;
}
foreach ( $wpide_remote['prefixes'] as $prefix ) {
if ( 0 === strpos( $postcode, strtoupper( $prefix ) ) ) {
$cart->add_fee( $wpide_remote['label'], $wpide_remote['amount'], $wpide_remote['taxable'] );
return;
}
}
} );
Notes
Postcodes match by prefix, so 'HS' covers HS1 to HS9; set 'taxable' to false if the fee should not be taxed.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.