You could try the following hook. This hook will check the customer’s shipping address during the checkout process and restrict purchases if the address is outside of the specified regions.
Here’s how you can implement this:
- Create a PHP file in your child theme directory: If you haven’t already, create a child theme to avoid losing your customizations when updating the main theme.
- Add the following code to your child theme’s functions.php file:
add_action( 'woocommerce_checkout_process', 'limit_shipping_to_delhi_ncr' );
add_action( 'woocommerce_after_checkout_validation', 'limit_shipping_to_delhi_ncr' );
function limit_shipping_to_delhi_ncr() {
// List of allowed postal codes for Delhi-NCR
$allowed_zip_codes = array(
'110001', '110002', '110003', '110004', '110005', // Example zip codes for Delhi
'201301', '201303', '201304', // Example zip codes for Noida
'122001', '122002', '122003' // Example zip codes for Gurgaon
);
$postcode = WC()->customer->get_shipping_postcode();
if ( ! in_array($postcode, $allowed_zip_codes) ) {
wc_add_notice( 'Sorry, we currently only ship to locations within Delhi-NCR.', 'error' );
}
}
Explanation:
- Hooks used:
woocommerce_checkout_process
and woocommerce_after_checkout_validation
are hooked to execute the custom function during the checkout process.
- Function
limit_shipping_to_delhi_ncr
: This function checks if the shipping postal code entered by the customer is in the allowed list. If it isn’t, an error message is displayed and the checkout process is halted.
- List of postal codes: Update the
$allowed_zip_codes
array with all the postal codes for the Delhi-NCR region where you want to allow shipping.
Customization:
You should customize the $allowed_zip_codes
list with all the specific postal codes for Delhi, Noida, Gurgaon, and other areas within the NCR region that you wish to cover.
This will effectively limit your WooCommerce store to only accept orders from customers in the Delhi-NCR area. Make sure to thoroughly test the checkout process to ensure that the restriction is working as expected.
Hope this helps