Skip to content

Instantly share code, notes, and snippets.

@clifgriffin
Created May 5, 2021 13:35
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 clifgriffin/42f87dd5f8debdbfbc0265b8dec3ed47 to your computer and use it in GitHub Desktop.
Save clifgriffin/42f87dd5f8debdbfbc0265b8dec3ed47 to your computer and use it in GitHub Desktop.
<?php
/**
* Prevent PO box shipping
*/
add_action( 'woocommerce_after_checkout_validation', 'deny_pobox_postcode' );
function deny_pobox_postcode( $posted ) {
$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( __( 'We Are Unable To Ship To PO Boxes. Please Enter Another Address' ) ), 'error' );
}
}
add_action( 'cfw_update_checkout_after_customer_save', 'deny_pobox_postcode_during_ajax_update' );
function deny_pobox_postcode_during_ajax_update() {
$address = WC()->customer->get_shipping_address();
$postcode = WC()->customer->get_shipping_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( __( 'We Are Unable To Ship To PO Boxes. Please Enter Another Address' ) ), 'error' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment