【问题标题】:Listing city taxonomy as woocommerce chekout shipping city dropdown将城市分类列为 woocommerce 结帐运输城市下拉列表
【发布时间】:2015-06-24 18:52:09
【问题描述】:

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

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

【问题讨论】:

    标签: php arrays wordpress woocommerce


    【解决方案1】:

    试试这个。

    只需更换

    • 用于循环

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

    • 这样设置选项

      'options' => array_values($cityArray)

    已经测试过,结果如下

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

    【讨论】:

    • 它的工作,但它生成选项值作为 0,1,2 而不是类别 slug 它生成什么 我需要的是
    • 只写下面的语句 var_dump($cityArray);在 foreach 循环右括号之后,让我知道结帐页面上的输出
    • 如果 var_dump 语句在结帐页面数组 (size=3) 'New_York_City' => string 'New York City' (length=13) 'Chicago' => string ' Chicago' (length=7) 'Dallas' => string 'Dallas' (length=6) 然后在您的情况下将 'options' => array_values($cityArray) 替换为 'options' => $cityArray
    猜你喜欢
    • 2018-04-11
    • 2020-08-12
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多