在 Woocommerce 中不需要结帐运输邮政编码字段

Exs*_*dor 2 php wordpress field checkout woocommerce

所以我按照以下说明操作:链接更改邮政编码的必填字段。

add_filter( 'woocommerce_billing_fields', 'wc_optional_billing_fields' );
function wc_optional_billing_fields( $address_fields ) {
    $address_fields['shipping_postcode']['required'] = false;

    return $address_fields;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,它没有更改所需的字段,而是创建了第二个字段: Image

谁能指出这里的问题是什么?

Loi*_*tec 7

That happens because you are using woocommerce_billing_fields hook filter that is managing only billing fields for a shipping postcode field…

You could try to use woocommerce_shipping_fields filter hook, but it will not work because the postcode checkout field is a very special field that can only be set to not required using the following hooked function:

add_filter( 'woocommerce_default_address_fields', 'customise_postcode_fields' );
function customise_postcode_fields( $address_fields ) {
    $address_fields['postcode']['required'] = false;

    return $address_fields;
}
Run Code Online (Sandbox Code Playgroud)

如您所见,它同时作用于计费和运输邮政编码字段。显然不可能使它仅用于计费和运输字段的运输邮政编码字段。

官方文档教程:使用操作和过滤器自定义结帐字段