Home » WooCommerce: Disallow Shipping to PO BOX Address

WooCommerce: Disallow Shipping to PO BOX Address

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 5
 * @donate $9     https://tutoraspire.com
 */
 
add_action( 'woocommerce_after_checkout_validation', 'tutoraspire_disallow_pobox_shipping' );
 
function tutoraspire_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' );
   }
}

You may also like