How to block PO Box shipping addresses in WooCommerce checkout

Many WooCommerce stores run into a common operational issue: certain products cannot be shipped to PO Boxes. Without validation in place, this can lead to failed deliveries, manual order intervention, and poor customer experience.

In this guide, we’ll walk through how to block PO Box addresses in WooCommerce checkout (shipping only) while still allowing them for billing—using server-side validation for reliability.

Issue Background

A WooCommerce store needed to prevent customers from entering PO Box addresses during checkout for shipping purposes.

  • Block PO Boxes only for shipping addresses
  • Allow PO Boxes for billing addresses
  • Display a clear error message to the user
  • Ensure validation works for both guest and logged-in users
  • Handle variations like “PO Box”, “P.O. Box”, and “P O Box”

Without this validation, orders could proceed with invalid shipping addresses, causing fulfillment issues.

Diagnosis

WooCommerce does not include built-in validation to restrict specific address formats like PO Boxes.

Client-side validation alone (JavaScript) is not sufficient because it can be bypassed and does not guarantee data integrity.

The correct approach is server-side validation during checkout processing. The appropriate hook for this is:

woocommerce_after_checkout_validation

This hook allows inspection of submitted checkout fields before the order is processed.

Resolution Steps

1. Hook into WooCommerce checkout validation

Add a custom function using the woocommerce_after_checkout_validation hook.

2. Target only shipping address fields

Focus specifically on:

  • shipping_address_1
  • shipping_address_2

Avoid applying validation to billing fields to ensure PO Boxes are still allowed there.

3. Detect PO Box patterns (case-insensitive)

/(p\.?\s*o\.?\s*box)/i

This will match common variations such as PO Box, P.O. Box, and P O Box.

4. Add server-side validation logic

add_action('woocommerce_after_checkout_validation', 'block_po_box_shipping', 10, 2);

function block_po_box_shipping($data, $errors) {
    $shipping_address_1 = isset($data['shipping_address_1']) ? $data['shipping_address_1'] : '';
    $shipping_address_2 = isset($data['shipping_address_2']) ? $data['shipping_address_2'] : '';

    $pattern = '/(p\.?\s*o\.?\s*box)/i';

    if (preg_match($pattern, $shipping_address_1) || preg_match($pattern, $shipping_address_2)) {
        $errors->add('validation', 'We are unable to ship to PO Boxes, please provide another shipping address.');
    }
}

5. Display a clear error message

WooCommerce automatically displays validation errors at checkout. Users will see a red error notice preventing checkout until the issue is corrected.

6. Test across scenarios

Be sure to test guest checkout, logged-in users, different PO Box formats, and both address lines.

Final Outcome

After implementing server-side validation:

  • PO Box shipping addresses are successfully blocked
  • Billing addresses remain unaffected
  • Validation is enforced reliably at the server level
  • Checkout experience remains clear and user-friendly
  • Fulfillment issues caused by invalid addresses are eliminated

Need help customizing WooCommerce checkout behavior? Contact Freshy.