Support » Plugin: WooCommerce » billing_state field required for Greece override

  • Resolved digiwebgreece

    (@digiwebgreece)


    Hello all,

    after many hours trying to override this field via functions.php on the template i found that For Greece on the file: woocommerce\includes\class-wc-countries.php
    the field state is not required:

    ‘GR’ => array(
    ‘state’ => array(
    ‘required’ => false,
    ),
    ),

    If i set this field to required => true and upload it the system works as expected so there is no issue with any plugin or template.

    I would like to solve this without a core hack of course and my two functions do not work do you have something to suggest perhaps?

    add_filter( ‘woocommerce_checkout_fields’, ‘custom_override_default_address_fields’ );
    function custom_override_default_address_fields($fields){
    global $woocommerce;
    $country = $woocommerce->customer->get_country();
    if($country == ‘GR’){
    $fields[‘billing’][‘state’][‘required’] = true;
    $fields[‘shipping’][‘state’][‘required’] = true;
    }
    return $fields;
    }

    add_filter( ‘woocommerce_billing_fields’, ‘formulare_filter_billing_state’, 10, 999 );
    function formulare_filter_billing_state( $address_fields ) {
    $address_fields[‘billing_state’][‘required’] = true;
    return $address_fields;
    }

Viewing 14 replies - 1 through 14 (of 14 total)
  • I also just tested this implementation bu updating the class-wc-countries.php with required true and the checkout looks ok with asterisk etc but once all the fields are selected except the state field (that is required) the calc update script is executed and brings a default shipping cost that is not actually true since if the user had selected a state there would be another shipping cost.

    BR,
    Alexandros

    Plugin Support Boro Sitnikovski

    (@bor0)

    Hi,

    Could you check if the following code snippet works for your case?

    
    <?php
    add_filter( 'woocommerce_get_country_locale', function( $locale ) {
    	$locale['GR']['state']['required'] = true;
    	return $locale;
    } );
    

    It worked. And if you want to have only one state?

    Thanks

    Hello, confirmed it is working, thank you very much!

    The only issue i see now is that the shipping costs script calculates the cost using standard shipment (invalid calculation) without state being selected.

    Any idea about correcting that perhaps?

    BR,
    Alexandros

    • This reply was modified 3 months, 1 week ago by  digiwebgreece.
    Plugin Support Three Sons

    (@3sonsdevelopment)

    Hi Alexandros,

    That’s a good question. You would need the shipping method to be aware of the state. Adding the code to make that happen would depend on the shipping method you’re using.

    The best thing to do would be to ask the developer of the shipping method how to take the state into consideration in the shipping costs. They should know if that’s possible and be able to help you out better.

    Take care,

    Hello,

    Thank you for your time and the reply.
    I believe it is a wordpress issue since the default shipping method in our case is standard shipping… there is only one alternative shipping method with other costs that is only activated if the state changes to some options… So since the state is not even selected don’t you think standard shipping should handle this properly?

    I mean this is a core thing right?

    Plugin Support Three Sons

    (@3sonsdevelopment)

    @digiwebgreece,

    While it is possible to make the field required, I don’t believe it adds in any extra functionality to track changes on the page. With the required tag, the checkout form will validate that field to make sure it is filled in. Making a change to that field won’t trigger a refresh of shipping rates.

    You would need to add-in some jQuery to trigger reloading. I haven’t tested this, but maybe something like:

    
    <script>
    jQuery(document.body).trigger("update_checkout");
    </script>
    

    You could add that to an HTML block on the checkout page itself. It’s also going to have to pick up a change that the shipping method can do something with. You would need to have rates set up with it in some way so setting the state adds in the charges you want.

    Take care,

    Hello, i think i got a bit confused about the reloading you suggested.

    It is not:
    a matter of incorrect calculation once the state is selected to trigger a reload in this case.

    It is: Shipping rate should not be visible until all fields including state are filled.
    Please note that currently the state field is Required and it is not working as required. You can see the page url i have provided in English language.

    So as you understand i am not sure that to do with the check out 😉

    Plugin Support Three Sons

    (@3sonsdevelopment)

    My apologies for overcomplicating this for you.

    So you want to hide the shipping rate until all fields have been filled in. There is an option in WooCommerce for that. Go to WooCommerce > Settings > Shipping > Shipping Options and check the box for “Hide shipping costs until an address is entered.”

    hide until complete

    Save your changes and see if that will make a difference for you. I haven’t tested it with a custom required field, but hopefully, the built-in option will work.

    Best,

    Hi, this field is already. enabled unfortunately thank you.
    Since i am a web dev i xan debug this with your guiding if you can of course.. but you after today i will be vac mode on 😉

    Plugin Support Three Sons

    (@3sonsdevelopment)

    Hi @digiwebgreece,

    I hope you’ve had a great vacation! Would it be possible to take a look at the site? It may be easier to work through this being able to actually interact with it.

    Thanks,

    Hello,
    The website is.
    https://verticalwise.shop/en/

    Thank you very much! 🙂

    • This reply was modified 2 months, 1 week ago by  digiwebgreece.
    Plugin Support Three Sons

    (@3sonsdevelopment)

    Howdy @digiwebgreece,

    My apologies. I have not had a chance to cook up a code snippet for you to use here.

    Here’s one way this could be approached. Use JavaScript to detect if the #billing_state or shipping_state select fields are filled in. If they are, then display tr.woocommerce-shipping-totals.shipping. Otherwise, hide it.

    That’s one way you could force the state to be selected before the shipping box is visible.

    Plugin Support Mike W

    (@nixiack8)

    Hi @digiwebgreece,

    I am going to close this thread for now, however if you are still having the issue just reply to this and we will re-open it.

    If you have other issues please open a new thread and let us know!

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.