Is there a way to display a custom message when the postal code entered is not covered by any shipping zone in woocommerce?

26 Views Asked by At

I have only one shipping zone available with a flat rate, which covers only a few zip codes within Sydney. I would like when entering any zip code that is not within that zone to display a custom message in the total section (where the shipping cost is shown), and to have the "place order" button disabled. Currently, what happens is that when customers enter a non-included zip code, the shipping cost field appears empty, and they receive an error message only after selecting "place order".

Thank you.

I have tried some codes as this:

// Validate
function ts_woocommerce_after_checkout_validation( $data, $error ) {
// The accepted delivery zones
$del_zones_array = array( 2000, 2170, 2007, 2008, 2009, 2010, 2011, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2033, 2032, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2059, 2060, 2061, 2062, 2063, 2064, 2065, 2066, 2067, 2068, 2069, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2079, 2080, 2081, 2082, 2084, 2085, 2086, 2087, 2088, 2089, 2090, 2110, 2111, 2112, 2113, 2114, 2115, 2116, 2117, 2118, 2119, 2120, 2121, 2122, 2125, 2126, 2127, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 2138, 2140, 2145, 2146, 2147, 2148, 2150, 2151, 2152, 2153, 2154, 2155, 2156, 2158, 2159, 2190, 2191, 2192, 2193, 2194, 2195, 2196, 2204, 2205, 2206, 2207, 2208, 2209, 2210, 2211, 2216, 2217, 2218, 2219, 2220, 2221, 2222, 2223, 2234, 2763, 2765, 2224, 2161, 2769, 2160, 2762, 2199, 2075, 2225, 2203 );

// If the postal is not within the array, deny checkout
if( ! in_array( $data\['shipping_postcode'\], $del_zones_array ) ) {
$error-\>add( 'validation', 'Sorry, we currently do not offer shipping to your area.' );
}
}
add_action('woocommerce_after_checkout_validation', 'ts_woocommerce_after_checkout_validation', 10, 2 );

But it doesn't meet what I want to achieve.

0

There are 0 best solutions below