如何防止邮政编码/邮政编码字段在 WooCommerce 中的某些国家/地区隐藏

即使在 WooCommerce 结帐页面上不使用邮政编码/邮政编码的国家/地区,我也想显示邮政编码/邮政编码字段。

默认情况下,WooCommerce 会为不使用它们的国家/地区隐藏邮政编码/邮政编码字段。

我在主题 functions.php 中使用了以下过滤器,但它不起作用。

add_filter( 'woocommerce_default_address_fields','custom_override_default_address_fields' );

function custom_override_default_address_fields( $address_fields ) {
     $address_fields['postcode']['hidden'] = false; 
     return $address_fields;
}

如何覆盖此行为?

zongjun0221 回答:如何防止邮政编码/邮政编码字段在 WooCommerce 中的某些国家/地区隐藏

您可以使用 woocommerce_get_country_locale 过滤器钩子,默认为所有国家/地区取消隐藏。

所以你得到:

function filter_woocommerce_get_country_locale( $country_locale ) { 
    // Loop through
    foreach( $country_locale as $key => $locale ) {
        // Isset
        if ( isset ( $locale['postcode']['hidden'] ) ) {
            // When true
            if ( $locale['postcode']['hidden'] == true ) {
                // Set to false
                $country_locale[$key]['postcode']['hidden'] = false;
            }
        }
    }

    return $country_locale;
}
add_filter( 'woocommerce_get_country_locale','filter_woocommerce_get_country_locale',10,1 );
本文链接:https://www.f2er.com/257884.html

大家都在问