Skip to content

Instantly share code, notes, and snippets.

@woogists
Last active November 30, 2021 10:37
Show Gist options
  • Save woogists/f542bf635c965fd965b37815f8928bc6 to your computer and use it in GitHub Desktop.
Save woogists/f542bf635c965fd965b37815f8928bc6 to your computer and use it in GitHub Desktop.
[General Snippets] Don’t allow PO BOX shipping
/**
* Prevent PO box shipping
*/
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode');
function 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' ) ) {
wc_add_notice( sprintf( __( "Sorry, we cannot ship to PO BOX addresses.") ) ,'error' );
}
}
@klishb
Copy link

klishb commented Jun 21, 2018

This code doesn't seem to work for me on a default WooCommerce checkout page. Can you confirm it still works? Also, not sure this line is intentional:

  • Set WooCommerce image dimensions upon theme activation

@klishb
Copy link

klishb commented Jun 21, 2018

whoops I guess it does work. I didn't realize the hook wasn't until you submit.

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