• Resolved Md Fathi Rahman

    (@fathi1999)


    I am trying to add a city dropdown in my Woocommerce store’s admin order edit area. I have tried this code.

    I have added places as an array. And here is the full code in Github.

    global $places;
    $places['BD'] = array(
        'dhaka' => array(
            __('Aam Bagan', 'woocommerce'),
            __('12 Tala', 'woocommerce'),
            __('Keraniganj Upazila Sadar', 'woocommerce'),
        ),
        'faridpur' => array(
            __('Alfadanga', 'woocommerce'),
        ),
        'gazipur' => array(
            __('Gazipur Sadar', 'woocommerce'),
            __('Kaliakair', 'woocommerce'),
        ),
        'gopalganj' => array(
            __('Gopalganj Sadar', 'woocommerce'),
            __('Kashiani', 'woocommerce'),
        ),
        'jamalpur' => array(
            __('Bakshiganj', 'woocommerce'),
            __('Dewanganj', 'woocommerce'),
        ),
    );
    
    function enqueue_scripts()
    {
        wp_enqueue_script('jquery');
        wp_enqueue_script('select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js', array('jquery'), '4.0.13', true);
        wp_enqueue_script('place-select-js', plugin_dir_url(__FILE__) . 'js/place-select.js', array('jquery', 'select2'), '0.1', true);
    }
    add_action('admin_enqueue_scripts', 'enqueue_scripts');
    
    
    add_filter('woocommerce_admin_billing_fields', 'admin_billing_city_select_field');
    function admin_billing_city_select_field($fields)
    {
        global $pagenow, $places;
    
        // Only for new order creation
        if ($pagenow != 'post-new.php') return $fields;
    
        $options = array('' => __('Select a city…', 'woocommerce'));
        if (isset($places)) {
            foreach ($places as $state => $cities) {
                foreach ($cities as $city => $city_name) {
                    $options[$city] = $city_name[0];
                }
            }
        }
    
        $fields['city'] = array(
            'label'   => __('City', 'woocommerce'),
            'show'    => false,
            'class'   => 'js_field-city select short',
            'type'    => 'select',
            'options' => $options,
        );
        return $fields;
    }

    Here in Admin pannel it’s returning only the first item from the Array list of places. I I chage the $options[$city] = $city_name[0]; to $options[$city] = $city_name; it’s giving a String “Array”

    How can I show all the city name based on state?

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Admin Order City dropdown based on state’ is closed to new replies.