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!

PHP Snippet: Disallow Shipping to PO BOX @ WooCommerce Checkout
/**
* @snippet Disallow Shipping to PO BOX
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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' );
}
}
This is not working for me, unfortunately, as it is still allowing me to enter a PO Box for the address.
Thanks for your comment Keith. Do you use a custom checkout template maybe?
Hello,
Can this be modified to add a filter for tagged products, so that only restricted products are disallowed from using the po box?
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!
It didnt work on the Post Code/ZIP fields. the order went thru even with PO BOX
You’re right, try this revised version
Works for me! Thank you so much
Cool
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
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!
Hi
Works great any idea about preventing shipping to units ( appartments )
Thanks!
Does this cover entering in PO BOX in the postal code,
Thanks
Yep!
Doesn’t work for Amazon Payments.
Hey Leon, thanks for your comment! Not sure how Amazon Payments work, sorry. Do they take you to Amazon servers?
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!
Ah, awesome Miroslav, thanks for your nice comment!
that was my EXACT experience…. Thank you Rodolfo!!!!
🙂
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?
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!
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.
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?
This is fabulous, and super helpful!
Cheers Sarah 🙂