How to get woocommerce country select dropdown?

你离开我真会死。 提交于 2019-12-04 11:54:18

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.

Braj Kishor Sah

Here is very simple and minified code:

global $woocommerce;    
woocommerce_form_field( 'billing_country', array( 'type' => 'country' ) );

You can add this to your function.php if you want custom country :

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!