WooCommerce – Filter shipping methods based on state

Below code snippet helps to filter shipping methods based on States.

In this particular example, the USPS rates will show up only when the user enters state from AK, HI, PR, GU, AS, VI, or UM ( convert it to appropriate state names please ) on cart/checkout page.

Also, please note that below code is written for the product WooForce USPS Shipping Plugin for WooCommerce. You can change the elements of the array eligible_services_for_states_list to the appropriate one to make it work with any other shipping plugin. By doing it, this will even work with any WooCommerce shipping API integrated plugins like FedEx, UPS, Canada Post etc.

Screenshots

Case 1: Notice that on WooCommerce cart page, CA (California) is chosen as state. And CA is NOT part of $states_list. So no USPS shipping methods are shown.
WooCommerce Cart - Without USPS Rates
Case 2:On WooCommerce cart page, AK (Alaska) is chosen as State. And AK is part of $states_list. So USPS shipping methods are displayed.
WooCommerce Cart - With USPS Rates


Leave a Reply