• Hello there,

    I am using your plugin on my website and it is working great and as expected.

    But I am making some custom code to use the Country / City / State elsewhere. So I am trying to create a function that retrieves cities and returns them in an array and I know that WooCommerce doesn’t support cities by default.

    For states, I am doing something like this, and I want to do the same for cities:

    /**
    * Fetch States
    */
    function fetch_states($country_key)
    {
        $states = WC()->countries->get_states($country_key);
    
        if (!$states) {
            return  [];
        };
    
        $state_list = [];
        foreach ($states as $key => $value) {
            $state_list[] = array(
                'code' => $key,
                'name' => $value
            );
        }
    
        return $state_list;
    }

    So whenever I try to call countries it fetches it’s states and “Cities”:

    $data = [];
    foreach ($countries as $key => $value) {
        $data[] = array(
            'code'   => $key,
            'name'   => $value,
            'states' => fetch_states($key),
            // Add Cities
        );

    Is there any way I can achieve that?

  • The topic ‘Fetch Cities from the plugin using a function or a hook’ is closed to new replies.