# Offer a Shipping Method Only to Listed Postcodes

> Shows a method such as local delivery or same-day courier only when the shipping postcode matches your list or starts with one of your prefixes.

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

## Code

```php
$wpide_local_rate_id   = 'flat_rate:3';                    // Rate ID to restrict.
$wpide_local_postcodes = array( '10115', '10117', '101*' ); // Exact postcodes or prefixes ending in *.

add_filter( 'woocommerce_package_rates', function ( $rates, $package ) use ( $wpide_local_rate_id, $wpide_local_postcodes ) {
    if ( ! isset( $rates[ $wpide_local_rate_id ] ) ) {
        return $rates;
    }

    $postcode = strtoupper( str_replace( ' ', '', (string) ( $package['destination']['postcode'] ?? '' ) ) );
    $allowed  = false;

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

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

    if ( '' === $postcode || ! $allowed ) {
        unset( $rates[ $wpide_local_rate_id ] );
    }

    return $rates;
}, 20, 2 );
```

## Notes

Use the rate ID shown in WooCommerce > Settings > Shipping (for example flat_rate:3); postcodes ending in * match by prefix.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Offer a Shipping Method Only to Listed Postcodes" 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/
