phpwordpresswoocommercehook-woocommercecheckout

Limit only shipping states (provinces) but not billing states in WooCommerce


I have the following code which limits the provinces to Valencia (Spain) only, in WooCommerce:

add_filter( 'woocommerce_states', 'tl45r_custom_woocommerce_states' );

function tl45r_custom_woocommerce_states( $states ) {

    // Only show specific provinces (Madrid - MD and Barcelona - B) for Spain (ES)
    $states['ES'] = array(
        //'MD' => __( 'Madrid', 'woocommerce' ),
        //'B'  => __( 'Barcelona', 'woocommerce' ),
        'V'  => __( 'Valencia', 'woocommerce' ),
    );

    return $states;
}

However, I want this limitation only be applied to the shipping field but not the billing field.

How can I do this?


Solution

  • To apply restriction only to shipping states, first you need to choose in WooCommerce "General" Settings, on Shipping location(s) the "Ship to specific countries only" option and in Ship to specific countries field, type "Spain" option (in your case), like:

    enter image description here

    Then replace woocommerce_states with woocommerce_countries_shipping_country_states filter hook, like:

    add_filter( 'woocommerce_countries_shipping_country_states', 'customized_shipping_country_states' );
    function customized_shipping_country_states( $states ) {
        // Only show specific provinces (Madrid - MD and Barcelona - B) for Spain (ES)
        $states['ES'] = array(
            //'MD' => __( 'Madrid', 'woocommerce' ),
            //'B'  => __( 'Barcelona', 'woocommerce' ),
            'V'  => __( 'Valencia', 'woocommerce' ),
        );
        return $states;
    }
    

    Now in checkout, only "Valencia" shipping state is available for Spain.

    This is what I get on Classic checkout (shortcode):

    enter image description here

    Note that it doesn't work for instance in Checkout Blocks.