Created
June 3, 2024 23:23
-
-
Save alexdeborba/2feaa5c8dac619db939d75c4134ca7a9 to your computer and use it in GitHub Desktop.
Prevent shipping to PO box addresses in WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Prevent shipping to PO box addresses in WooCommerce | |
*/ | |
add_action('woocommerce_after_checkout_validation', 'deny_pobox_postcode'); | |
function deny_pobox_postcode($posted) { | |
// Determine if we are checking the shipping or billing address | |
$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']; | |
// Characters to replace | |
$replace = array(' ', '.', ','); | |
// Clean and normalize the address and postcode | |
$address = strtolower(str_replace($replace, '', $address)); | |
$postcode = strtolower(str_replace($replace, '', $postcode)); | |
// Check if the address or postcode contains 'pobox' | |
if (strpos($address, 'pobox') !== false || strpos($postcode, 'pobox') !== false) { | |
wc_add_notice(__('Sorry, we cannot ship to PO BOX addresses.', 'woocommerce'), 'error'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment