Skip to content

Instantly share code, notes, and snippets.

@simonlk
Created June 22, 2014 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonlk/8c8acd029c4e001da591 to your computer and use it in GitHub Desktop.
Save simonlk/8c8acd029c4e001da591 to your computer and use it in GitHub Desktop.
WooCommerce - Don't allow shipping to PO Box addresses
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." );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment