Disallow shipping to Parcel Lockers & P.O. Boxes in WooCommerce

P.O. Boxes and Parcel Lockers are typically operated by a country’s national mail service, making them inaccessible to stores that use private couriers to deliver goods. Fortunately, WooCommerce makes is easy to reject P.O. Box or Parcel Locker numbers entered into the checkout.

To block Parcel Locker and P.O. boxes from your WooCommerce store, just add the following code to your theme’s Functions.php file:

//DENY p.o. boxes and parcel lockers
 add_action('woocommerce_after_checkout_validation', 'mpz_deny_pobox_postcode');

function mpz_deny_pobox_postcode( $posted ) {
 global $woocommerce;

  $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( $postcode, 'pobox' ) || strstr( $address, 'parcellocker' ) || strstr( $address2, 'parcellocker' ) || strstr( $address2, 'pobox' ) || strstr( $postcode, 'parcelcollect' ) || strstr( $address, 'parcelcollect' )  || strstr( $address2, 'parcelcollect' ) ) {
   $notice = sprintf( __( '%1$sSorry, our courier is unable to deliver to Parcel Lockers or P.O. Boxes.' , 'error' ) , '<strong>' , '</strong>' );
            wc_add_notice( $notice, 'error' );
  }
}

This code is based on the Don’t Allow PO BOX shipping example in the WooCommerce documentation, but has been expanded to also block Parcel Lockers.

Shane Gowland

Hi! I’m a web designer, software developer, and digital marketer based in South Australia. I've been building websites on WordPress for over 12 years. You can learn more about the projects and businesses I'm involved with on my website.

Leave a Reply