The ability to hide shipping methods based on the presence of PO Boxes in address fields is an easy option now. The code snippets let you hide shipping options when the customer enters a PO Box in the shipping address.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
add_filter('woocommerce_package_rates', 'ts_disable_shipping_for_po_box', 10, 2);
function ts_disable_shipping_for_po_box($rates, $package) {
// Check if the 'PO BOX' is present in the shipping address
$shipping_address = $package['destination']['address'];
if (strpos($shipping_address, 'PO BOX') !== false) {
// If 'PO BOX' is present, unset all shipping methods
$rates = array();
}
return $rates;
}
add_filter('woocommerce_package_rates', 'ts_disable_shipping_for_po_box', 10, 2);
function ts_disable_shipping_for_po_box($rates, $package) {
// Check if the 'PO BOX' is present in the shipping address
$shipping_address = $package['destination']['address'];
if (strpos($shipping_address, 'PO BOX') !== false) {
// If 'PO BOX' is present, unset all shipping methods
$rates = array();
}
return $rates;
}
add_filter('woocommerce_package_rates', 'ts_disable_shipping_for_po_box', 10, 2); function ts_disable_shipping_for_po_box($rates, $package) { // Check if the 'PO BOX' is present in the shipping address $shipping_address = $package['destination']['address']; if (strpos($shipping_address, 'PO BOX') !== false) { // If 'PO BOX' is present, unset all shipping methods $rates = array(); } return $rates; }
Output
If a customer inputs a PO Box in the address fields, the available shipping options will not be displayed.
Other than PO Box, if any other address is entered it will show the default available shipping methods.
Instead of hiding shipping methods based on address fields, you can also hide a shipping method based on product and state field in WooCommerce.
Re move the P. O box
Hi Glydine,
Just to clarify that we understand your requirement correctly.
Are you looking to completely prevent customers from entering a PO Box address during checkout (by showing an error and prevents checkout), instead of just hiding the shipping options ?