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 5
 * @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'];
   $postcode = ( isset( $posted['shipping_postcode'] ) ) ? $posted['shipping_postcode'] : $posted['billing_postcode'];
   $replace = array( " ", ".", "," );
   $address = strtolower( str_replace( $replace, '', $address ) );
   $address2 = strtolower( str_replace( $replace, '', $address2 ) );
   $postcode = strtolower( str_replace( $replace, '', $postcode ) );
   if ( strstr( $address, 'pobox' ) || strstr( $address2, 'pobox' ) || strstr( $postcode, 'pobox' ) ) {
      wc_add_notice( 'Sorry, we do not ship to PO BOX addresses.', 'error' );
   }
}

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its 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 guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went 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.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent 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.

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

  1. This is not working for me, unfortunately, as it is still allowing me to enter a PO Box for the address.

    1. Thanks for your comment Keith. Do you use a custom checkout template maybe?

  2. Hello,

    Can this be modified to add a filter for tagged products, so that only restricted products are disallowed from using the po box?

    1. Hey Josh, 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!

  3. It didnt work on the Post Code/ZIP fields. the order went thru even with PO BOX

    1. You’re right, try this revised version

  4. Works for me! Thank you so much

    1. Cool

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

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

    1. Thanks!

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

    Thanks

    1. Yep!

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

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

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

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

  12. This is fabulous, and super helpful!

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

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

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