在特定国家/地区的woocommerce中显示州的文本字段

时间:2020-04-14 22:08:28

标签: php wordpress woocommerce

在woocommerce中,有些国家根本看不到“州”字段,我希望能够在特定国家/地区将其显示为必填文本字段。

我尝试了一些我在这里找到的解决方案,但它们似乎都是旧文章,或者至少我无法使其正常工作。

我发现同一个片段有两个非常相似的版本,一个在结帐页面上引发了很多错误,另一个是上面的那个,根本行不通,它添加了要在所有国家/地区发货的文本字段,并且不只是法国:

function xa_filter_woocommerce_states( $states ) { 
    unset( $states['FR'] );
    var_dump( $states ) ;
    return $states;
};
add_filter( 'woocommerce_states', 'xa_filter_woocommerce_states', 10, 1 );

function xa_filter_woocommerce_get_country_locale( $locale ) { 
    $locale['FR']['state']['required'] = true;
    return $locale; 
};
add_filter( 'woocommerce_get_country_locale', 'xa_filter_woocommerce_get_country_locale', 10, 1 );

这工作正常,可以满足我的需要,但是它会在结帐页面上抛出一些文本(似乎不是错误),如下所示:

array(76){[“” AF“] => array(0){} [” AO“] => array(18){[”“ BGO”] =>字符串(5)“ Bengo” [“ BLU “] => string(8)” Benguela“ [” BIE“] => string(4)”Bié“ [” CAB“] => string(7)” Cabinda“ [” CNN“] => string(6) “ Cunene” [“ HUA”] =>字符串(6)“ Huambo” [“ HUI”] =>字符串(6)“Huíla” [“ CCU”] =>字符串(14)“ Kuando Kubango” [“ CNO” ] =>字符串(12)“ Kwanza-Norte” [“ CUS”] =>字符串(10)“ Kwanza-Sul” [“ LUA”] =>字符串(6)“ Luanda” [“ LNO”] =>字符串(11)“ Lunda-Norte” [“ LSU”] =>字符串(9)

1 个答案:

答案 0 :(得分:0)

我发现上面的代码可以正常工作,并且屏幕上的“错误”不是错误,而是来自变量的转储。因此,删除上述代码的1行即可达到目的:

function xa_filter_woocommerce_states( $states ) { 
    unset( $states['FR'] );
    return $states;
};
add_filter( 'woocommerce_states', 'xa_filter_woocommerce_states', 10, 1 );

function xa_filter_woocommerce_get_country_locale( $locale ) { 
    $locale['FR']['state']['required'] = true;
    return $locale; 
};
add_filter( 'woocommerce_get_country_locale', 'xa_filter_woocommerce_get_country_locale', 10, 1 );