【问题标题】:wc_countries - states selection dropdown - woocommercewc_countries - 状态选择下拉列表 - woocommerce
【发布时间】:2016-04-17 21:06:29
【问题描述】:

我正在努力设置一个带有默认 WooCommerce 国家和州选择下拉列表的表单。

基本上,我想显示国家选择,然后根据国家选择显示州选择。因此,如果用户选择英国,则会显示带有州选择的新下拉菜单。

我已经做到了:

<?php
global $woocommerce;
$countries_obj   = new WC_Countries();
$countries   = $countries_obj->__get('countries');
$default_country = $countries_obj->get_base_country();
$default_county_states = $countries_obj->get_states( $default_country );

echo '<div id="ships_from_countries_field">' . __('Countries') . '';

    woocommerce_form_field('my_country_field', array(
           'type'       => 'select',
           'class'      => array( 'chzn-drop' ),
           'label'      => __('Item ships from - country'),
           'placeholder'    => __('Select a Country'),
           'options'    => $countries
            )
     );
 echo '</div>';

 echo '<div id="ships_from_state_field">' . __('States') . '';

     woocommerce_form_field('my_state_field', array(
            'type'       => 'select',
            'class'      => array( 'chzn-drop' ),
            'label'      => __('Item ships from - state'),
            'placeholder'    => __('Select a State'),
            'options'    => $default_county_states
             )
      );
  echo '</div>';
  ?>

国家下拉菜单显示国家和州下拉菜单显示商店基地的状态 - 英国

但是我如何让它们一起工作并保存值呢?

找不到任何信息,有人有做过这项工作的经验吗?

【问题讨论】:

  • 老实说,我很困惑。为什么你认为你已经做到了? WooCommerce 开箱即用 - 这是自定义表单吗?或者你在哪个页面上做这个?
  • 那我该如何实现默认的WC功能呢?你能指出我正确的方向吗?我试图复制结帐页面并从那里获取它,但这似乎有点太混乱了。这段代码是我在搜索如何让国家和州选择工作几天后发现的唯一东西..
  • 你在哪个页面上做这个?你到底想做什么?
  • 它是我的其他插件模板页面之一。该插件提供从前端编辑产品的功能。在这个页面上,我需要放置一个国家选择下拉菜单和州选择下拉菜单。

标签: javascript php jquery wordpress woocommerce


【解决方案1】:

首先,国家和州都有一个类型..你不需要做太多......就像这样......

echo '<div id="ships_from_countries_field">' . __('Countries') . '';

    woocommerce_form_field('my_country_field', array(
       'type'       => 'country',
       'class'      => array( 'chzn-drop' ),
       'label'      => __('Item ships from - country'),
       'placeholder'    => __('Select a Country')
        )
    );
echo '</div>';

echo '<div id="ships_from_state_field">' . __('States') . '';

    woocommerce_form_field('my_state_field', array(
        'type'       => 'state',
        'class'      => array( 'chzn-drop' ),
        'label'      => __('Item ships from - state'),
        'placeholder'    => __('Select a State')
        )
    );
echo '</div>';

注意他们的类型......他们不是类型选择。

现在你的问题..

PHP

你需要有国家和州的本地化脚本...我不会继续如何使用wp_localize_script()。如果需要,请阅读链接。这是其中的一部分。

$wc_country = array(
    'country' => json_encode( array_merge( WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states() ) )
);
wp_localize_script( 'my-js', 'my_js', $wc_country );

javascript

这样,在您的 my-js 脚本(一个脚本文件)中,您可以像这样读取状态:

    var states_json = my_js.countries.replace( /&quot;/g, '"' ),
        states = $.parseJSON( states_json );

然后您必须在您的国家/地区添加更改事件并根据选择的国家/地区重新填充状态...

例如:states["PH"] 如果选择的国家是菲律宾

00: "Metro Manila"
ABR: "Abra"
AGN: "Agusan del Norte"
AGS: "Agusan del Sur"
AKL: "Aklan"
ALB: "Albay"
ANT: "Antique"
APA: "Apayao"
AUR: "Aurora"
BAN: "Bataan"
BAS: "Basilan"
BEN: "Benguet"
BIL: "Biliran"
.....
WSA: "Samar"
ZAN: "Zamboanga del Norte"
ZAS: "Zamboanga del Sur"
ZMB: "Zambales"
ZSI: "Zamboanga Sibugay"

然后你可以使用 jQuery 来构建你的选项..

var options = '',
    state = states[ country ]; // country can be: var country = $('country').val();

for( var index in state ) {
    if ( state.hasOwnProperty( index ) ) {
        options = options + '<option value="' + index + '">' + state[ index ] + '</option>';
    }
}

【讨论】:

  • Reigel,我真的非常感谢您在这方面提供的帮助。我一直在寻找解决方案,我会着手解决并回复结果。非常感谢您再次接受你的时间..
  • 这个必须被标记为接受的答案。您不仅给出了想法,还给出了完整的解决方案:)
  • var states_json = my_js.countries 应该像 var states_json = my_js.country
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 2012-03-09
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多