【问题标题】:How to prevent postcode/zip field from getting hidden for some countries in WooCommerce如何防止在 WooCommerce 中的某些国家/地区隐藏邮政编码/邮政编码字段
【发布时间】:2021-06-14 11:13:30
【问题描述】:

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

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

如何覆盖此行为?

【问题讨论】:

    标签: php wordpress woocommerce checkout postal-code


    【解决方案1】:

    您可以使用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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      相关资源
      最近更新 更多