In our store, we have the UPS and USPS shipping methods.
The products we sell are of all different shapes and sizes, so we have to use the APIs, and not table rate options.
I only want the USPS rates to show up when the user enters a zip code from AK, HI, PR, GU, AS, VI, or UM. I already tried the Conditional Shipping and Payments method but it doesn’t seem to work. If I’m not wrong, that would only work anyway for the rates/options returned at checkout and not for the cart, which I need.
I’ve browsed through many different pages online where this question was asked, and tried editing the functions.php
file to match the responses, but all of the answers seem to be outdated.
The closest answer I can find is on option #2 at the link below, however it doesn’t work when I try it: http://support.wooforce.com/hc/en-us/articles/207515625-Hiding-disabling-a-shipping-service
Here is the code I have customized from that link and that I have tried to make it work. Notice that I want to exclude the method for every state except Alaska, Hawaii, Puerto Rico, Guam, American Samoa, Virgin Islands, and the minor islands.
Any help is appreciated!
add_filter('woocommerce_package_rates', 'wf_hide_undesired_service_for_required_states', 10, 2);
function wf_hide_undesired_service_for_required_states($rates, $package)
{
$exclude = array(
'wf_shipping_usps:D_PRIORITY_MAIL' => array(
'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'
)
);
if (is_array($exclude)) {
foreach($exclude as $shipping_method => $excluded_states) {
if (in_array(WC()->customer->shipping_state, $excluded_states)) {
unset($rates[$shipping_method]);
}
}
}
return $rates;
}
As per my understanding, you want to show the USPS realtime rates only when customer chooses the state AK, HI, PR, GU, AS, VI, or UM.
Solution is explained in detail here.
http://www.xadapter.com/2016/06/17/woocommerce-filter-shipping-methods-based-on-state/
Below code snippet will work with out any changes for WooForce USPS Shipping Plugin. If you are using WooCommerce USPS plugin of any other Vendor, you can make it work by changing the elements of the variable
$eligible_services_for_states_list
to the appropriate values relevant for that plugin.Hope it helps.