Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ofernandolopes/514f19b1951d9769a41f to your computer and use it in GitHub Desktop.
Save ofernandolopes/514f19b1951d9769a41f to your computer and use it in GitHub Desktop.
WooCommerce - Don't Ship to PO Boxes
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( "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