Hello Dan,
Sure, there is a way to achieve this using this plugin as a starting point.
First, make sure there’s no shipping method assigned for either of the rural shipping zones you’ll see at WooCommerce > Settings > Shipping (these zones are generated when the plugin is activated). At this point, you’ll see if you try to add a rural postcode in the shipping calculator WooCommerce will display a message, “No shipping options were found”.
Next, add the following code to your theme’s functions.php file:
/**
* Check if rural postcode and display message.
*
* @return string
*/
function chthnc_set_message_shipping_not_available() {
if ( ! is_admin() ) {
$shipping_packages = WC()->cart->get_shipping_packages();
$shipping_zone = wc_get_shipping_zone( reset( $shipping_packages ) );
$zone_name = $shipping_zone->get_zone_name();
if ( 'North Island (Rural)' === $zone_name || 'South Island (Rural)' === $zone_name ) {
return __( 'Sorry, we do not ship to rural addresses.' );
}
}
}
add_filter( 'woocommerce_no_shipping_available_html', 'chthnc_set_message_shipping_not_available' );
add_filter( 'woocommerce_cart_no_shipping_available_html', 'chthnc_set_message_shipping_not_available' );
This little script will do a couple of things:
- Check if the selected shipping zone is a rural one
- Replace the generic WooCommerce message with a custom message, “Sorry, we do not ship to rural addresses.”
As an extra step, it’s worth making your postcode form field a required field (if not already), to ensure a customer doesn’t try to slip through a rural address.
I hope this helps.