添加葡萄牙的州字段并使其成为 WooCommerce Checkout 中的必填项

问题描述 投票:0回答:1

我正在构建我的 WooCommerce 商店,需要更改结帐账单/送货字段。目前,它显示葡萄牙国家/地区的州/县为可选,但对我来说重要的是它是必需的并标有红色星号,因为这会影响价格。

我尝试过 WooCommerce 的 Checkout Field Editor 等插件,但没有成功。还尝试了一些我在网上找到的代码,我将其添加到子主题的functions.php中:

add_filter( 'woocommerce_billing_fields', 'ts_require_wc_state_field');
function ts_require_wc_state_field( $fields ) {
    $fields['billing_state']['required'] = true;
    return $fields;
}

add_filter( 'woocommerce_default_address_fields', 'custom_override_default_address_fields');
function custom_override_default_address_fields( $address_fields ) 
{
    $address_fields['billing_state']['required'] = true;
    return $address_fields;
}

两个代码片段都不起作用。

也许有人可以帮我找到正确的儿童主题代码?

在这方面有点新手,所以非常感谢任何帮助。

php wordpress woocommerce checkout country
1个回答
0
投票

对于一些国家,比如葡萄牙,需要一些不同的东西才能发挥作用。尝试以下操作:

add_filter( 'woocommerce_get_country_locale', 'change_portugal_country_locale', 100 );
function change_portugal_country_locale( $locale ) {
    $locale['PT']['state'] = array(
        'required' => true,
        'hidden'   => false,
        'priority' => 93, // state field after the postcode field
    );
    $locale['PT']['country'] = array(
        'priority' => 96, // country field after the state field
    );
    return $locale;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并可使用以下代码。

但是由于 葡萄牙的 WooCommerce 中未定义州,因此需要您在以下函数中定义每个州名称(及其相关的州代码):

add_filter( 'woocommerce_states', 'potugal_states_woocommerce' );
function potugal_states_woocommerce( $states ) {
    $states['PT'] = array(
        'AAB' => __('State one aab', 'woocommerce') ,
        'BBC' => __('State two bbc', 'woocommerce') ,
        'CCD' => __('State three ccd', 'woocommerce') ,
    );
    return $states;
}

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

你会得到类似的东西:

和:

说明: 某些国家/地区在 WooCommerce 中具有特定的“区域设置”字段设置。我们正在添加/更改在

WC_Countries
get_country_locale()
方法

上定义的默认 WooCommerce 国家/地区区域设置

相关:在 WooCommerce 中为越南国家/地区设置州结账字段为必填项

© www.soinside.com 2019 - 2024. All rights reserved.