将城市分类法列为 woocommerce 结帐航运城市下拉列表

Listing city taxonomy as woocommerce chekout shipping city dropdown

我已经通过以下方法成功添加了自定义结帐城市下拉字段

$fields['shipping']['shipping_city'] = array(
    'label'       => __('City', 'woocommerce'),
    'placeholder' => _x('', 'placeholder', 'woocommerce'),
    'required'    => true,
    'clear'       => true,
    'type'        => 'select',
    'class'       => array('own-css-name'),
    'options'     => array(
        'New_York_City' => __('New York City', 'woocommerce' ),
        'Chicago' => __('Chicago', 'woocommerce' ),
        'Dallas' => __('Dallas', 'woocommerce' )
        )
    );

我想从城市分类法中获取选项值,所以我尝试了以下方法,但它似乎不起作用 (http://i.stack.imgur.com/dasIm.jpg)

    $args = array( 
     'child_of' => 0,
     'type'     => 'product',
     'taxonomy' => 'city',
     'hide_empty' => 0
     ); 
    $categories = get_categories( $args );
    foreach ($categories as $category) {
    $cityArray[] = "'".$category->slug."' => __('".$category->name."', 'woocommerce' )";
    }
    $fields['shipping']['shipping_city2'] = array(
        'label'       => __('City', 'woocommerce'),
        'placeholder' => _x('', 'placeholder', 'woocommerce'),
        'required'    => true,
        'clear'       => true,
        'type'        => 'select',
        'class'       => array('own-css-name'),
        'options'     => array(implode( ', ', $cityArray ) )
 );

试试这个。

只需更换

  • For 循环

    foreach ($categories as $category) { $cityArray[$category->slug] = $category->名称; }

  • 像这样设置选项

    'options' => array_values($cityArray)

同样进行了测试,结果如下

如果这对你有用,请告诉我。