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
 * @community     https://businessbloomer.com/club/
 */
 
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 custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Remaining $$$ to Get Free Shipping Notification @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. So, here’s how we show a simple message on […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]

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. Follow @rmelogli

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? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. 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 the Business Bloomer Club to get quick WooCommerce support. Thank you!

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