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 ErrorWooCommerce: 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' );
   }
}
Share:

Leave a Reply