Do not allow PO BOX address to be used on shipping for Woocommerce

0
81

I had a task from a client to make a script not to allow shipping to PO BOX address from Woocommerce.

This can be done by using and adding the following code on functions.php on the theme folder. I recommend using always a child theme if you do regular updates to the theme:

add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode_function');

function deny_pobox_postcode_function( $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, 'p.o. box' ) || strstr( $address, 'po box' ) || strstr( $address, 'po. box' ) ) {
        wc_add_notice( sprintf( __( "We cannot ship to PO BOX addresses.") ) ,'error' );
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here