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.
11 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:
2