WooCommerce limit shipping method based on state

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.

Read More

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;
}

Related posts

Leave a Reply

1 comment

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

    add_filter( 'woocommerce_package_rates', 'xa_show_shipping_method_based_on_state', 10, 2 );
    
    function xa_show_shipping_method_based_on_state( $available_shipping_methods, $package ) {
    
        $states_list = array( 'AK', 'HI', 'PR', 'GU', 'AS', 'VI', 'UM' );
    
        $eligible_services_for_states_list  =   array(
                'wf_shipping_usps:flat_rate_box_priority',
                'wf_shipping_usps:flat_rate_box_express',
                'wf_shipping_usps:D_FIRST_CLASS',
                'wf_shipping_usps:D_EXPRESS_MAIL',
                'wf_shipping_usps:D_STANDARD_POST',
                'wf_shipping_usps:D_MEDIA_MAIL',
                'wf_shipping_usps:D_LIBRARY_MAIL',
                'wf_shipping_usps:D_PRIORITY_MAIL',
                'wf_shipping_usps:I_EXPRESS_MAIL',
                'wf_shipping_usps:I_PRIORITY_MAIL',
                'wf_shipping_usps:I_GLOBAL_EXPRESS',
                'wf_shipping_usps:I_FIRST_CLASS',
                'wf_shipping_usps:I_POSTCARDS',         
            );
    
        // Basically, below code will reset shipping services if the shipping
        // state is other than defined states_list.
        if ( !in_array(WC()->customer->shipping_state, $states_list) ) {
            foreach ( $eligible_services_for_states_list as &$value ) {
                unset( $available_shipping_methods[$value] );       
            }
        }
    
        return $available_shipping_methods;
    }
    

    Hope it helps.