将城市分类列为woocommerce chekout shipping city dropdown

时间:2015-06-24 18:52:09

标签: php arrays wordpress woocommerce

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

$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 ) )
 );

1 个答案:

答案 0 :(得分:1)

试试这个。

只需替换

即可
  • For Loop with

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

  • 设置此选项

    '选择' => array_values($ cityArray)

已经测试了相同的结果,如下所示

enter image description here

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