我已经尝试了几天,或者我遗漏了一个明显的(可能/可能)的东西,Woocommerce在地址格式方面存在一个(不严重的)错误(可能但不太可能) ),或者依靠默认格式(可能)根本无法实现我想做的事情,所以我不介意有人为我看这个东西。
最终目标是如下所述格式化WC地址。
'AF' => array(),
) Address_1 (form-row-wide)
Town (form-row-first) Postcode (form-row-last)
Address_1 (form-row-first) Town/City (form-row-last)
State (form-row-first) Postcode (form-row-last)
NB:我已经尝试过的所有方法都意识到,其中邮政编码或城市不在地址范围之内的地址格式不适合上述地址格式(例如,新加坡不需要城市,而阿联酋则不需要没有邮政编码)。我还没有研究这些用例,因为我正在努力使上述格式(涵盖大多数国家)正常工作。
我得到的结果充其量是不一致的。我在下面详细介绍了一些。
Image showing results(绿色表示该情况的100%,红色突出显示了对于指定格式不正确的内容)
下面显示产生结果的代码。我已经尝试了许多排列,但似乎无法获得一种有效的排列。
add_filter( 'woocommerce_default_address_fields', 'AF_woocommerce_default_address_fields', 100, 1 );
function AF_woocommerce_default_address_fields( $fields ) {
$fields['state']['required'] = false;
$fields['state']['hidden'] = true;
$fields['state']['label'] = 'this is the default label for state';
$fields['state']['class'] = array('form-row-first', 'address-field' );
unset( $fields['address_2'] );
unset( $fields['company'] );
$fields['city']['class'] = array( 'form-row-first', 'address-field' );
$fields['postcode']['class'] = array( 'form-row-last', 'address-field' );
return $fields;
}
add_filter( 'woocommerce_get_country_locale', 'AF_woocommerce_get_country_locale', 100, 1 );
function AF_woocommerce_get_country_locale( $locale ) {
$woo_countries = new WC_Countries();
$states = $woo_countries->get_states();
foreach( $locale as $country => $locale_details ) {
if( isset( $states[$country] ) ) {
if( ! empty( $states[$country] ) ) {
$locale[$country]['state']['required'] = true;
$locale[$country]['state']['label'] = 'this is state label';
$locale[$country]['state']['hidden'] = false;
$locale[$country]['city']['class'] = array( 'form-row-last', 'address-field' );
$locale[$country]['state']['class'] = array( 'form-row-first', 'address-field' );
$locale[$country]['address_1']['class'] = array( 'form-row-first', 'address-field' );
$locale[$country]['postcode']['class'] = array( 'form-row-last', 'address-field' );
}
}
}
return $locale;
}
我开始认为,我不再依赖于我需要在所有国家/地区填充和排列(在woocommerce_get_country_locale
中)并为每个国家/地区指定格式的默认格式,尽管这样做似乎很容易喜欢做事的错误方式。
有人有没有遇到过这个问题,或者对我所缺少的东西有任何想法?
谢谢
EDIT:似乎甚至没有为woocommerce_get_country_locale
中的每个国家/地区设置格式(忽略传入的内容并返回一个全新的数组)。 Image showing results of this
EDIT2:认为WC中存在错误,或者根本就没有代码来解决有关国家变更的类-见下文。
进一步的调查显示,当国家/地区更改时,js不会应用这些类。使用以下代码,如果存在一个州列表,则所有国家/地区将显示州字段(根据需要),而当不存在该列表时,所有国家/地区将不显示州字段。
add_filter( 'woocommerce_states', 'AF_woocommerce_states', 10, 1 );
function AF_woocommerce_states( $states ){
$woo_countries = new WC_Countries();
$wc_countries = $woo_countries->get_countries();
foreach( $wc_countries as $country => $country_name ) {
if( ! isset( $states[$country] ) ) {
$states[$country] = array();
}
}
return $states;
}
add_filter( 'woocommerce_default_address_fields', 'FA_woocommerce_default_address_fields' );
function FA_woocommerce_default_address_fields( $fields ) {
unset( $fields['company'] );
unset( $fields['address_2'] );
return $fields;
}
add_filter( 'woocommerce_get_country_locale', 'AF_woocommerce_get_country_locale' );
function AF_woocommerce_get_country_locale( $locale ) {
$woo_countries = new WC_Countries();
$wc_states = $woo_countries->get_states();
foreach( $wc_states as $country => $state_details ) {
if( empty( $state_details ) ) {
$locale[$country]['state']['required'] = false;
} else {
if( isset( $locale[$country] ) && $locale[$country]['state']['required'] === false ) {
$locale[$country]['state']['required'] = true;
}
}
}
return $locale;
}
如果然后通过将上面的最后一个过滤器(woocommerce_get_country_locale)更改为下面的代码,将分类添加到每个国家/地区,则会发生以下情况。 NB-仍然忽略那些在几分钟内重新排列州/邮政编码字段的国家。 在输入/刷新带有需要州(例如,州列表)的国家/地区的结帐信息时,格式正确为
address_1 (form-row-first) city (form-row-last)
state (form-row-first) postcode (form-row-last)
从以上内容(例如美国)更改为需要州(例如,存在州列表-例如NZ)的其他国家/地区,格式仍然如上并且正确。 如果您从美国或新西兰更改为不需要州的国家(例如英国),那么州字段将被隐藏,但是js不会将英国类应用于其余字段,格式变为
address_1 (form-row-first) city (form-row-last)
<blank space where state was> postcode (form-row-last)
应该在什么时候
address_1 (form-row-wide)
city (form-row-first) postcode (form-row-last)
当您使用不需要州名的国家/地区输入结帐/刷新时,情况也相反,则格式正确为
address_1 (form-row-wide)
city (form-row-first) postcode (form-row-last)
更改为不需要状态的其他国家/地区后,格式仍然存在。更改为需要州的国家/地区后,格式变为
address_1 (form-row-wide)
city (form-row-first) <blank space>
state (form-row-wide
<blank space> postcode (form-row last)
下面的代码
add_filter( 'woocommerce_get_country_locale', 'AF_woocommerce_get_country_locale' );
function AF_woocommerce_get_country_locale( $locale ) {
$woo_countries = new WC_Countries();
$wc_states = $woo_countries->get_states();
foreach( $wc_states as $country => $state_details ) {
if( empty( $state_details ) ) {
$locale[$country]['state']['required'] = false;
$locale[$country]['address_1']['class'] = array( 'form-row-wide', 'address-field' );
$locale[$country]['city']['class'] = array( 'form-row-first', 'address-field' );
$locale[$country]['postcode']['class'] = array( 'form-row-last', 'address-field' );
} else {
if( isset( $locale[$country] ) && $locale[$country]['state']['required'] === false ) {
$locale[$country]['state']['required'] = true;
}
$locale[$country]['address_1']['class'] = array( 'form-row-first', 'address-field' );
$locale[$country]['city']['class'] = array( 'form-row-last', 'address-field' );
$locale[$country]['state']['class'] = array( 'form-row-first', 'address-field' );
$locale[$country]['postcode']['class'] = array( 'form-row-last', 'address-field' );
}
}
return $locale;
}