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.
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.