• Resolved Advex

    (@totalfly)


    hi, i’m trying to do something that should be simple but actually for who is not a programmer it’s quite complicated.

    in my checkout form i need that the country will be a required and validate field.

    after a long research in internet, i could remove the “option” text from the form and mark the field as required with the following code

    `add_filter( ‘woocommerce_checkout_fields’ , ‘totalfly_not_required_fields’, 9999 );

    function totalfly_not_required_fields( $f ) {

    $f[‘billing’][‘billing_country’][‘required’] = true; // that’s it

    // the same way you can make any field required, example:
    // $f[‘billing’][‘billing_company’][‘required’] = true;

    return $f;
    }’

    but actually the field still not validate, i mean the customers can checkout even without select the country.

    Please could you help me telling how can i make the field validate, i mean necessary to proceed with the order confirmation, and if is not select will appear an error message like in the case of the state field? I’s quite urgent.

    thanks a lot

Viewing 8 replies - 1 through 8 (of 8 total)
  • I was unable to get country to be optional. What countries do you sell to?

    On my setup, ‘country’ is always required by default. I found it difficult to place an order where the country was given as “default” which is not what we want.

    Instead of setting required to true, try setting validate, so:

    add_filter( 'woocommerce_checkout_fields', 'totalfly_validate_country', 9999 );
    function totalfly_validate_country( $fields ) {
      $fields['billing']['billing_country']['validate'] = array ( 'country' );
      return $fields;
    }
    • This reply was modified 4 years, 5 months ago by Majeed Raza. Reason: Changed answer
    Thread Starter Advex

    (@totalfly)

    thank you very much, it doesn’t work..
    if i do not delect the country woocommerce allow me to send the order

    i don’t know why he do not allow me to have the country field as a required field in the moment that the country define a lot of variations, for example taxes, shipping costs, etc etc etc..

    any other suggestion please?

    What countries do you sell to please?

    Thread Starter Advex

    (@totalfly)

    the shop is to sell in any country..

    I’ve been looking at this. It seems that ‘default’ is an acceptable country. I think WooCommerce expects you to set a default country. However, this would allow an error as you have found. If the customer does not set their country, do they want the default country or have they just forgotton to set one?

    So I think the thing to do would be to validate the country and throw an error if it is set to ‘default’.

    add_action( 'woocommerce_after_checkout_validation', 'custom_validate_country', 10, 2 );
    function custom_validate_country( $data, $errors ) {
      if( 'default' == $data['billing_country'] ) {
        $errors->add( 'validation', 'Please select a billing country' );
      } else {
        if( 'default' == $data['shipping_country'] ) {
          $errors->add( 'validation', 'Please select a shipping country' );
        }
      }
    }

    Works for me but your setup may be different.

    Thread Starter Advex

    (@totalfly)

    the snippet is working, thank a lot.. finally after days going around the problem now i have a solution..

    the only one problem is that now if i select the country, the state field is not required anymore.. ??

    i mean.. if i select a country where is not defined the state field ok, if i select a country where the state field is listed, this is not required anymore..

    btw it can work also like this
    thanks a lot

    WooCommerce handles this situation. If your site doesn’t, its likely that you have an issue with your theme or one of your plugins.

    Setup your site with the Storefront theme and only the WooCommerce plugin. It should work now. Works for me like this. Then reinstate your theme and plugins one by one and test to find out which one is stopping the state field from changing to suit the country.

    Plugin Support Thu P. a11n

    (@thup90)

    Hi there,

    I hope you found the previous reply helpful. 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.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Country required and validate’ is closed to new replies.