Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Last active January 15, 2018 19:58
Show Gist options
  • Save mikejolley/2282666 to your computer and use it in GitHub Desktop.
Save mikejolley/2282666 to your computer and use it in GitHub Desktop.
WooCommerce - Don't allow PO BOX within postcode, address 1 or address 2 fields.
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function deny_pobox_postcode( $posted ) {
global $woocommerce;
// Put postcode, address 1 and address 2 into an array
$check_address = array();
$check_address[] = isset( $posted['shipping_postcode'] ) ? $posted['shipping_postcode'] : $posted['billing_postcode'];
$check_address[] = isset( $posted['shipping_address_1'] ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
$check_address[] = isset( $posted['shipping_address_2'] ) ? $posted['shipping_address_2'] : $posted['billing_address_2'];
// Implode address, make lowercase, and remove spaces and full stops
$check_address = strtolower( str_replace( array( ' ', '.' ), '', implode( '-', $check_address ) ) );
if ( strstr( $check_address, 'pobox' ) ) {
$woocommerce->add_error( "Sorry, we don't ship to PO BOX addresses." );
}
}
@mkmbmw
Copy link

mkmbmw commented May 18, 2012

Hi, Mike. Where shell I put these codes to make it work? Thank you

@imfromio
Copy link

Hi Mike,
When I add this snippet to my theme's function.php file, I get this fatal error: Using $this when not in object context in... This is caused by $this on line 6 of your code. What am I doing wrong?
Thanks.

@tonydjukic
Copy link

When I add the code to the functions.php of the theme, the PLACE ORDER button just leaves me with a spinning AJAX loop and doesn't progress any further. This occurs whether I use a PO Box or not.

Is there anything else we need to add to get this to work?

@EranSch
Copy link

EranSch commented Jan 8, 2013

Just spent some time trying to figure out an implementation of this. For anyone else having issues, this snippet should be added to your THEME'S functions.php file. Assuming you're in the US and you want to be checking against the "Address 1" field, the code above should be modified to check 'shipping_address_1'

The $this error seems to be easily solved as well. Basically, copy/paste this into your functions.php file and you're all set.

add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');

function deny_pobox_postcode( $posted ) {
 global $woocommerce;

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

 $postcode = strtolower(str_replace( ' ', '', $postcode ));

 if ( strstr( $postcode, 'pobox' ) ) {
   $woocommerce->add_error( "Sorry, we cannot ship to PO BOX addresses." );
 }
}

All credit goes to imfromio and samueljeden's thread on at:
http://wordpress.org/support/topic/do-not-allo-po-boxes

@thewzrd
Copy link

thewzrd commented Feb 7, 2013

Thanks for this code snippet - it looks like it works perfectly. My question/problem is that some users have PO Box in their second shipping address field... how would you modify this code to check all the shipping fields for PO Box? Thanks so much.

@eappell
Copy link

eappell commented Apr 17, 2013

@thewzrd - There's probably a much more efficient way to write this, but if you want the quick n dirty, this works for me using either address field:

/**
 * Prevent PO Box from being used for shipping
 **/
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');

function deny_pobox_postcode( $posted ) {
    global $woocommerce;    
    $postcode = ( isset( $posted['shipping_address_1'] ) ) ? $posted['shipping_address_1'] : $posted['billing_address_1'];
    $postcode = strtolower(str_replace( ' ', '', $postcode ));

    $postcode2 = ( isset( $posted['shipping_address_2'] ) ) ? $posted['shipping_address_2'] : $posted['billing_address_2'];
    $postcode2 = strtolower(str_replace( ' ', '', $postcode2 ));

    if ( strstr( $postcode, 'pobox' ) || strstr( $postcode2, 'pobox' ) ) {
        $woocommerce->add_error( "Sorry, we cannot ship to PO BOX addresses." );
    }
}

@mikejolley
Copy link
Author

Updated to check address 1,2 and postcode and trim full stops.

@AutoIveL
Copy link

So, does this not work with Free Shipping enabled?

@beesmart
Copy link

Hi Mike,

The code outputs an incorrect error message 'Internal Server Error', apparently $woocommerce->add_error(); has been deprecated. Though it of course still functions correctly.

So replace:

$woocommerce->add_error( "Sorry, we cannot ship to PO BOX addresses." );

With:

wc_add_notice( sprintf( __( "Sorry, we cannot ship to PO BOX addresses.") ) ,'error' );

Also if you know how I could implement strstr to check an array instead of a string as I'm having a bit of trouble getting it to work, trying to exclude multiple postcodes.

strstr( $check_address, $example_array )

Thanks!

@IFBDesign
Copy link

Mike,

Is there a way to use this code to where the message pops up only when they try to use a certain shipping method? For example the UPS method. I want to deny shipping to PO Box Addresses based on the shipping method UPS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment