To ensure accurate deliveries, you want to restrict customers from entering certain ZIP codes during checkout. This code ensures that customers can only proceed with checkout if they enter a ZIP code from the PIN codes listed in the code.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Validate
function ts_woocommerce_after_checkout_validation( $data, $error ) {
// The accepted delivery zones
$del_zones_array = array( 560043, 560038, 560025 );
// If the postal is not within the array, deny checkout
if( ! in_array( $data['shipping_postcode'], $del_zones_array ) ) {
$error->add( 'validation', 'The ZIP you provided is not available for online deliveries.' );
}
}
add_action('woocommerce_after_checkout_validation', 'ts_woocommerce_after_checkout_validation', 10, 2 );
// Validate
function ts_woocommerce_after_checkout_validation( $data, $error ) {
// The accepted delivery zones
$del_zones_array = array( 560043, 560038, 560025 );
// If the postal is not within the array, deny checkout
if( ! in_array( $data['shipping_postcode'], $del_zones_array ) ) {
$error->add( 'validation', 'The ZIP you provided is not available for online deliveries.' );
}
}
add_action('woocommerce_after_checkout_validation', 'ts_woocommerce_after_checkout_validation', 10, 2 );
// Validate function ts_woocommerce_after_checkout_validation( $data, $error ) { // The accepted delivery zones $del_zones_array = array( 560043, 560038, 560025 ); // If the postal is not within the array, deny checkout if( ! in_array( $data['shipping_postcode'], $del_zones_array ) ) { $error->add( 'validation', 'The ZIP you provided is not available for online deliveries.' ); } } add_action('woocommerce_after_checkout_validation', 'ts_woocommerce_after_checkout_validation', 10, 2 );
Output
The below output shows that customers will be shown a validating message and also avoid proceeding with the order if the PIN Code is not in the predefined list of shipping postcodes.
A similar error message can be shown when none of the shipping method is selected by the customer and instead tries to place an order just with the pre-selected shipping option. Refer to this post that will help you to make shipping method fields mandatory on WooCommerce checkout page.