-
-
Save mikejolley/2282666 to your computer and use it in GitHub Desktop.
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." ); | |
} | |
} |
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!
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.
So, does this not work with Free Shipping enabled?