Posted on

WooCommerce: Disallow Shipping to PO BOX Address

Today we take a look at the WooCommerce Checkout page and our goal is to disallow placing an order to customers that enter a PO BOX address. I don’t remember where I got this snippet from, but either way I’m glad to share it again!

WooCommerce: Disallow PO BOX Shipping and Display Error
WooCommerce: Disallow PO BOX Shipping and Display Error

WooCommerce Snippet: Disallow Shipping to PO BOX

 * @snippet       Disallow Shipping to PO BOX
 * @how-to        Watch tutorial @
 * @sourcecode
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.5.1

add_action( 'woocommerce_after_checkout_validation', 'bbloomer_disallow_pobox_shipping' );

function bbloomer_disallow_pobox_shipping( $posted ) {
 global $woocommerce;
 $address  = ( isset( $posted['shipping_address_1'] ) ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
 $address2  = ( isset( $posted['shipping_address_2'] ) ) ? $posted['shipping_address_2'] : $posted['billing_address_2'];
 $replace  = array(" ", ".", ",");
 $address  = strtolower( str_replace( $replace, '', $address ) );
 $address2 = strtolower( str_replace( $replace, '', $address2 ) );
 if ( strstr( $address, 'pobox' ) || strstr( $address2, 'pobox' ) ) {
     wc_add_notice( 'Sorry, we do not ship to PO BOX addresses.', 'error' );

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

10 thoughts on “WooCommerce: Disallow Shipping to PO BOX Address

  1. Hey Rodolfo,

    thanks for the code (again!). I found a similar one on the official WooCommerce website, but it didn’t really work for PayPal payments. But yours did! Thanks!

    1. Ah, awesome Miroslav, thanks for your nice comment!

    2. that was my EXACT experience…. Thank you Rodolfo!!!!

  2. Hi Rodolfo,

    I’m new to wordpress and found your post through a Google search. Where exactly are you pasting the snipet? Is there a specific file?

    1. Hey Nicholas, yes you can place this in your child theme’s functions.php file – if you need more guidance, please take a look at this video tutorial: Hope this helps!

  3. I have copied and pasted your code exactly and it doesn’t seem to be working for me – any insight would be appreciated. It just continues on to the payment stage.

    1. Hey Emma, thanks so much for your comment! This should work with the default 1-page checkout – are you using a multi-step checkout plugin instead?

  4. This is fabulous, and super helpful!

    1. Cheers Sarah ๐Ÿ™‚

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.