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:

add_action('woocommerce_after_checkout_validation', 'gm_deny_pobox_postcode');

function gm_deny_pobox_postcode( $posted ) {
 global $woocommerce;

  $address  = ( isset( $posted['shipping_address_1'] ) ) ?     
 $posted['shipping_address_1'] : $posted['billing_address_1'];

 $postcode = ( isset( $posted['shipping_postcode'] ) ) ?  
 $posted['shipping_postcode'] : $posted['billing_postcode'];

 $replace  = array(" ", ".", ",");
 $address  = strtolower( str_replace( $replace, '', $address ) );
 $postcode = strtolower( str_replace( $replace, '', $postcode ) );

 if ( strstr( $address, 'pobox' ) || strstr( $postcode, 'pobox' ) || strstr( $address, 'parcellocker' ) ) {
   $notice = sprintf( __( '%1$sSorry, we are 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 Reply