# Block PO Box Shipping Addresses

> Rejects checkout when the shipping address is a PO Box or post office locker, for couriers that cannot deliver there.

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

## Code

```php
$wpide_po_patterns = '/\b(p\.?\s*o\.?\s*box|post\s*office\s*box|pobox|packstation|postfach)\b/i'; // Patterns to block.

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

    $prefix = ! empty( $data['ship_to_different_address'] ) ? 'shipping_' : 'billing_';
    $lines  = ( $data[ $prefix . 'address_1' ] ?? '' ) . ' ' . ( $data[ $prefix . 'address_2' ] ?? '' );

    if ( preg_match( $wpide_po_patterns, $lines ) ) {
        $errors->add( 'shipping', 'Sorry, we cannot deliver to PO Boxes or parcel lockers. Please enter a street address.' );
    }
}, 10, 2 );
```

## Notes

Checks both address lines of the address the order ships to; extend $wpide_po_patterns for other languages.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Block PO Box Shipping Addresses" 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/
