• Resolved mappel

    (@mappel)


    I’m looking for a way to remove specific billing_state fields. For UK/ GB or switzerland for example. To get rid of fields that are not ‘required’.

    I tried this but this isn’t working:

    function mp_change_locale_field_defaults($countries) {
    $countries[‘GB’][‘state’][‘hidden’] = true;
    return $countries;
    }

    I looked into class-wc-countries.php as well but I also did not find the line where I can manually edit this.

    Help is very much appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Nicola Mustone

    (@nicolamustone)

    Automattic Happiness Engineer

    Hello there,
    You can find here a document that explains how to remove fields from the checkout form: https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2

    This can get you started with your idea.

    Thread Starter mappel

    (@mappel)

    Hi Nicola,
    thanks for getting back to me.
    Great, I successfully removed all billing state fields with the following code

    add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’ );
    function custom_override_checkout_fields( $address_fields ) {
    unset($address_fields[‘billing’][‘billing_state’]);
    return $address_fields;
    }

    Can you point me in the right direction on how to apply this for the a specific country?
    I want to get rid of state/county for UK/GB and switzerland for example.
    I don’t know how to implement the depended select billing_country scenario.

    Warm regards

    Nicola Mustone

    (@nicolamustone)

    Automattic Happiness Engineer

    If you want it to be dependent on the country selection you would probably need to use JavaScript instead of PHP like the above script.

    I do not have an example for that though. You might find something on this Google search.

    Thread Starter mappel

    (@mappel)

    Hey, I hope you are safe and healthy.
    Unfortunately, I did not find a solution on the google search.

    Isn’t there a more easy solution?
    Why make it dynamic (true, more elegant) when I can delete the code in the relevant .php file. I guess it’s class-wc-countries.php

    So my idea: look up switzerland or UK and delete the condition that defines that the billing state for those countries is relevant and shows up on the checkout page.

    You know what I mean? There must be a .php file that tells woocommerce on checkout what to do when
    The rule/condition must be set up somewhere, right? Why not go there and get rid of that condition in the first place.

    Unfortunately (again) I did not find any code that looked like this condition.
    When UK show billing state.

    Can you point me in the right direction.
    Warm regards

    Remi Corson

    (@corsonr)

    Automattic Happiness Engineer

    Hey,

    Have you checked this solution, it seems to be quite close to what you’re trying to achieve:

    https://www.wpdesk.net/blog/woocommerce-checkout-conditional-fields/

    Thread Starter mappel

    (@mappel)

    Hi Remi,
    thanks for the link but unfortunately it doesn’t really do what I am looking for.

    Can I ask you where exactly the checkout conditions/rules for the dependent country and billing_state fields are located?

    I thought class-wc-countries.php is where you can find those conditions and rules. https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-countries.php It’s in the WooCommerce – includes folder.

    In lines 482pp (with germany as an example)

    public function get_address_formats() {
        if ( empty( $this->address_formats ) ) {
            $this->address_formats = apply_filters(
                'woocommerce_localisation_address_formats',
                array(
                    'default' => "{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}",
    
                    'DE'      => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}", [...]

    So I tried to add

    'GB' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",

    no n{state}

    This did not work unfortunately

    Strangely there is the line for switzerland

    'CH' => "{company}\n{name}\n{address_1}\n{address_2}\n{postcode} {city}\n{country}",

    No state in the code but on the checkout the billing_state will show up. I’m looking at the wrong place I guess.

    I tried another approach:

    In line 1262pp

    'GB' => array(
                        'postcode' => array(
                            'label' => __( 'Postcode', 'woocommerce' ),
                        ),
                        'state'    => array(
                            'label'    => __( 'County', 'woocommerce' ),
                            'required' => false,

    OK, why not try to insert

     'state'    => array(
                        'label'    => __( 'County', 'woocommerce' ),
                        'required' => false,
                        'hidden'   => true,

    Nope.

    Can you point me in the right direction where this billing state condition/rule (dependend on from which country the user is) is set up?

    Of course, I don’t want you to write the code for me but if you could be so kind and tell me which .php is relevant.

    Just as a reminder: I want to get rid of the billing_state field for specific countries (for those cases the billing_state isn’t a required field anyway… I just want to streamline and minimize the checkout process)

    Thanks so much for your patience!

    I hope you are well and safe.
    Warm regards

    • This reply was modified 5 years, 11 months ago by mappel.

    I got what you need. Lost the original source to credit but use this code. Add this to your themes functions.php

    add_filter( 'woocommerce_states' , 'keep_specific_country_states', 10, 1 );
    function keep_specific_country_states( $states ) {
        // HERE define the countries where you want to keep
        $countries = array('US', 'AU', 'CA');
        $new_country_states = array();
    
        // Loop though all country states
        foreach( $states as $country_code => $country_states ){
            if( ! in_array( $country_code, $countries ) ){
                // Remove states from all countries except the defined ones
                $states[$country_code] = array();
            }
        }
        return $states;
    }

    Add countries that require a state to;

    $countries = array('US', 'AU', 'CA');

    • This reply was modified 5 years, 10 months ago by skrillaking.
    • This reply was modified 5 years, 10 months ago by skrillaking.
    Remi Corson

    (@corsonr)

    Automattic Happiness Engineer

    We haven’t heard back from you in a while, so I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    I think its better to do it by JS.

    
    // hide country field for UK
    $(document.body).on("updated_checkout", function (e) {
        var sm_selected_country = $("#billing_country").val();
        console.log("selected county", sm_selected_country);
        if (sm_selected_country && sm_selected_country === "GB") {
            $("#billing_state_field").addClass("d-none");
        } else {
            $("#billing_state_field").removeClass("d-none");
        }
    });
    
    • This reply was modified 5 years, 4 months ago by coder618.
Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘How to remove county field (or billing_state) on checkout if country is UK/GB?’ is closed to new replies.