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.

Rows of aluminum mail boxes for concepts such as safety and security, business communication and concepts. Shallow depth of field.

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.

Leave a Comment