WooCommerce Checkout 必填字段与计费城市字段有关

WooCommerce Checkout required fields issue with billing city field

我有一个 woocommerce 商店。最近我在结帐账单表格中制作必填字段时遇到问题。

我有这个定制:

'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,
),

在woocommerce/includes/class-wc-countries.php路径中, 如您所见:

'required'     => true,

但是没有显示星号 (*) 或 <abbr> html 标签被添加到我的表单中。

对上述问题有什么想法吗?

此致。

没有任何保证,因为它可能取决于许多因素,例如您的主题、第三方插件或自定义...

官方文档: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.

例如你不需要在你的class属性中添加'address-field''validate-required' 因为它们是自动添加的。此外,此帐单字段的优先级已经 70

所以您应该尽量只保留需要更改的属性:

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;
} 

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

代码在 WooCommerce 中经过测试和工作。

此代码将以这种方式制作城市账单字段: