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

PHP Snippet: Disallow Shipping to PO BOX @ WooCommerce Checkout

/**
 * @snippet       Disallow Shipping to PO BOX
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_action( 'woocommerce_after_checkout_validation', 'bbloomer_disallow_pobox_shipping' );
 
function bbloomer_disallow_pobox_shipping( $posted ) {
   $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 snippet?

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 files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

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 Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

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

  1. Hi Rodolfo – this code is exactly what I have been searching for. Thank you.

    Just a question – how could I check for either “P O Box” or “Private Bag” in each of the 2 address lines – we have 2 kinds of boxes in this country and I cannot courier to either. Just wondered how to check for multiple strings on the same line.

    Thanks, Cat

    1. Hi Cat, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hi
    Works great any idea about preventing shipping to units ( appartments )

    1. Thanks!

  3. Does this cover entering in PO BOX in the postal code,

    Thanks

    1. Yep!

  4. Doesn’t work for Amazon Payments.

    1. Hey Leon, thanks for your comment! Not sure how Amazon Payments work, sorry. Do they take you to Amazon servers?

  5. 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!!!!

  6. 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: https://businessbloomer.com/woocommerce-customization-hangout/. Hope this helps!

  7. 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?

  8. This is fabulous, and super helpful!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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