Listing city taxonomy as woocommerce chekout shipping city dropdown

I have managed to add custom checkout city dropdown field by following method

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

I want to get the option values from city taxonomy so I have tried the following method but it doesn’t seems to work (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 ) )
 );

Related posts

1 comment

  1. Try this .

    Just replace the

    • For Loop with

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

    • Set option like this

      ‘options’ => array_values($cityArray)

    Have tested the same and the results where as follows

    enter image description here

    Let me know if this worked for you.

Comments are closed.