Skip to content

Instantly share code, notes, and snippets.

@wbcomdev
Created May 17, 2021 05:38
Show Gist options
  • Select an option

  • Save wbcomdev/27a280e138e4efbc6d0f85538400ca34 to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/27a280e138e4efbc6d0f85538400ca34 to your computer and use it in GitHub Desktop.
/**
* Prevent PO box shipping.
*/
function wb_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' );
}
}
add_action( 'woocommerce_after_checkout_validation', 'wb_deny_pobox_postcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment