wordpress 如何获得woocommerce国家选择下拉菜单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31046974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get woocommerce country select dropdown?
提问by Jagankumar
I want to display woocommerce countries list some where on website. how can i get the country list like this as image?
我想在网站上显示 woocommerce 国家/地区列表。我怎样才能得到这样的国家名单作为图像?


回答by WisdmLabs
Yes you can achieve this by having the following code where ever you want
是的,你可以通过在任何你想要的地方使用以下代码来实现这一点
global $woocommerce;
$countries_obj = new WC_Countries();
$countries = $countries_obj->__get('countries');
echo '<div id="my_custom_countries_field"><h2>' . __('Countries') . '</h2>';
woocommerce_form_field('my_country_field', array(
'type' => 'select',
'class' => array( 'chzn-drop' ),
'label' => __('Select a country'),
'placeholder' => __('Enter something'),
'options' => $countries
)
);
echo '</div>';
I have tested the same and have used the same code in a shortcode and used that shortcode on product description
我已经测试了相同的代码并在短代码中使用了相同的代码,并在产品描述中使用了该短代码


Let me know if this works for you too.
让我知道这是否也适用于您。
回答by Braj Kishor Sah
Here is very simple and minified code:
这是非常简单和缩小的代码:
global $woocommerce;
woocommerce_form_field( 'billing_country', array( 'type' => 'country' ) );
回答by colapsnux
You can add this to your function.php if you want custom country :
如果您想要自定义国家/地区,您可以将其添加到您的 function.php 中:
add_filter( 'woocommerce_checkout_fields', 'add_custom_select_country' );
function add_custom_select_country( $fields ) {
$fields['billing']['billing_select_country'] = array(
'type' => 'select',
'required' => true,
'clear' => false,
'options' => array(
'country' => __('Country', 'woocommerce' ),
'fr' => __('France', 'woocommerce' ),
'gb' => __('United Kingdom', 'woocommerce' ),
'ru' => __('Russian', 'woocommerce' )
)
);
return $fields;
}

