WooCommerce Checkout required fields issue with billing city field

Hya.

I have a woocommerce shop. Recently I got the problem with making required field in checkout billing form.

I Have maid this customization:

'city' => array( 'label' => __( 'City Name', 'woocommerce' ), 'placeholder' => __( 'City Name'), 'required' => true, 'class' => array( 'form-row-wide', 'address-field', 'validate-required' ), 'validate' => array( 'city' ), 'autocomplete' => 'address-level2', 'priority' => 70,
),

in woocommerce/includes/class-wc-countries.php path, as you see:

'required' => true,

But NO asterisk is displayed (*) or <abbr> html tag is added to my form.

Any Idea about above question?

Regards.

1

1 Answer

Without any guaranty as it can depend on many things like your theme, third party plugins or customizations…

Official documentation: Customizing checkout fields using actions and filters

Instead of editing all the properties for this "city" checkout field, you should just override the necessary ones.

For example you don't need to add in your class property: 'address-field' and 'validate-required' as they are automatically added. Also the priority is already 70 for this billing field…

So you should try to keep only the properties that you need to change:

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields', 100, 1 );
function custom_override_default_address_fields( $address_fields ) { $address_fields['city']['label'] = __( 'City Name', 'woocommerce' ); $address_fields['city']['placeholder'] = __( 'City Name'); $address_fields['city']['required'] = true; return $address_fields;
} 

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

The code tested and works in WooCommerce.

This code will make city billing field this way:

enter image description here

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like