# Accept Orders Only for Your Delivery Area

> Stops checkout with a friendly message when the delivery postcode is outside the area you serve, ideal for local food, flower or furniture delivery.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, checkout, postcodes, local-delivery
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-delivery-postcode-whitelist/

## Code

```php
$wpide_area_postcodes = array( 'SW1*', 'SW3*', 'W8*' ); // Postcodes or prefixes ending in *.
$wpide_area_message   = 'Sorry, we only deliver within central London at the moment.';

add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) use ( $wpide_area_postcodes, $wpide_area_message ) {
    if ( empty( WC()->cart ) || ! WC()->cart->needs_shipping() ) {
        return;
    }

    $prefix   = ! empty( $data['ship_to_different_address'] ) ? 'shipping_' : 'billing_';
    $postcode = strtoupper( str_replace( ' ', '', (string) ( $data[ $prefix . 'postcode' ] ?? '' ) ) );

    foreach ( $wpide_area_postcodes as $rule ) {
        $rule = strtoupper( str_replace( ' ', '', $rule ) );

        if ( '*' === substr( $rule, -1 ) ? 0 === strpos( $postcode, rtrim( $rule, '*' ) ) : $postcode === $rule ) {
            return;
        }
    }

    $errors->add( 'shipping', esc_html( $wpide_area_message ) );
}, 10, 2 );
```

## Notes

Postcodes ending in * match by prefix, and orders that do not need shipping are never checked.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Accept Orders Only for Your Delivery Area" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
