【问题标题】:How to get woocommerce country select dropdown?如何获得 woocommerce 国家/地区选择下拉菜单?
【发布时间】:2015-06-25 09:55:05
【问题描述】:

我想在网站的某些位置显示 woocommerce 国家/地区列表。我怎样才能得到这样的国家列表作为图像?

【问题讨论】:

    标签: wordpress woocommerce e-commerce


    【解决方案1】:

    是的,你可以通过在任何你想要的地方使用以下代码来实现这一点

    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>';
    

    我已经测试过相同的代码,并在简码中使用了相同的代码,并在产品描述中使用了该简码

    让我知道这是否也适合你。

    【讨论】:

    • 您的代码显示所有国家/地区。但我想显示我在设置页面上指定的特定国家(卖给特定国家)
    • 使用键(这将成为下拉选项的值)和值(这将在该选项的下拉列表中显示为文本)以数组格式获取这些国家,并将其分配给 $countries 变量代码。
    • woocommerce_form_field 有一个 type => country 参数,这个参数确实准确,你在找什么.. 见 Braj Kishor Sah 的回答..
    【解决方案2】:

    这里是非常简单和缩小的代码:

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

    【讨论】:

    • 如果 $key 设置为'shipping_country'(如示例中所示),它将拉出商店将运送到的所有国家/地区。如果设置为其他内容,它将拉出所有允许国家/地区的列表(均可调整@woocommerce->settings->general)。整个woocommerce_form_field 函数是here
    【解决方案3】:

    如果您想要自定义国家/地区,可以将其添加到您的 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-13
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      • 2013-02-27
      • 1970-01-01
      • 2020-04-04
      相关资源
      最近更新 更多