Skip to content

Instantly share code, notes, and snippets.

@rynaldos-zz
Created October 12, 2017 13:11
Show Gist options
  • Save rynaldos-zz/b70ed83e28ffe313a65938640f32c70b to your computer and use it in GitHub Desktop.
Save rynaldos-zz/b70ed83e28ffe313a65938640f32c70b to your computer and use it in GitHub Desktop.
[WooCommerce 3.0+] Preventing 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' );
}
}
@draney
Copy link

draney commented Nov 15, 2017

This doesn't prevent the order from going through for us. It does give them the notice "Sorry, we cannot ship to PO BOX addresses." but the order has already gone through. I'm wondering if the timing is correct? Seems like woocommerce_after_checkout_validation might be too late in process. Is there anything like woocommerce_before_checkout_validation that might work better?

@daschenbrener
Copy link

This doesn't prevent the order from going through for us. It does give them the notice "Sorry, we cannot ship to PO BOX addresses." but the order has already gone through. I'm wondering if the timing is correct? Seems like woocommerce_after_checkout_validation might be too late in process. Is there anything like woocommerce_before_checkout_validation that might work better?

any luck with a solution?

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