WordPress woocommerce_get_country_locale hook not working

I am trying to use the filter woocommerce_get_country_locale to remove the “State” field for a specific country. However, it does not work.

Here is what I tried:

Read More

Attempt 1

add_filter(
    'woocommerce_get_country_locale',
    static function (array $locale): array {
        $locale['HK']['state']['hidden'] = true;
        return $locale;
    }
);

Attempt2

apply_filters(
    'woocommerce_get_country_locale',
    [
        'HK' => [
            'postcode' => [
                'required' => false,
            ],
            'city' => [
                'label' => __('Town / District', 'woocommerce'),
                // 'placeholder' => __( 'Town / District', 'woocommerce' )
            ],
        ],
    ]
);

Please suggest how to achieve this. Thank you!

Related posts

1 comment

  1. You could try adding to your array:

            'state' => array(
               'required' => false
             )
    

    Like France in the function, for example.

    So your filter would look like

        apply_filters( 'woocommerce_get_country_locale', array(
                   'HK' => array(
                    'postcode' => array(
                      'required' => false
                    ),
                    'city'  => array(
                    'label'       => __( 'Town / District', 'woocommerce' ),
                    //'placeholder' => __( 'Town / District', 'woocommerce' )
                    )
                    'state' => array(
                      'required' => false
                    )
                )
            ));
    

    The function which controls this lives in

    woocommerce/includes/class-wc-countries.php

    It could be removed there but you would need to take great care to ensure that this is maintained when you update WooCommerce. I would not recommend editing core theme files in this way but it can produce the desired result.

    This might help in providing good background to the get country locale function:
    http://woocommerce.wp-a2z.org/oik_api/wc_countriesget_country_locale/?bwscid1=2

    And this might provide another way – showing that that country is not allowed if you wanted to remove HK altogether:
    http://woocommerce.wp-a2z.org/oik_api/wc_countriesget_allowed_countries/

    Something in the answers here might help to point you in a more correct way to do it:
    https://wordpress.stackexchange.com/questions/73062/how-to-force-wordpress-to-temporarily-switch-locale-using-qtranslate

    This also might explain some of the behaviour: https://wordpress.stackexchange.com/questions/120741/cant-change-a-label-in-woocommerce-with-the-normal-filter

Comments are closed.