VandaG
VandaG

Reputation: 21

WooCommerce dropdown list of cities in My Account page

I am trying to add a dropdown and successfully did it on checkout page by following this code,

but I am not able to do it for My Account page. I know that the code is different for My Account, but can't figure it out how to adapt.

I am looking at this code. I added some version of it and now it does show a dropdown and the only option is 'array', not a list of cities.

Here is my code:

// Billing and Shipping fields on my account edit-addresses and checkout

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {

    $fields['city']['options'] = array(
    '' => __( 'Select your city' ),
    'Burnaby' => 'Burnaby',
    'Coquitlam' => 'Coquitlam',
    'Langley' => 'Langley',
    'New Westminster' => 'New Westminster',
    'North Vancouver' => 'North Vancouver',
    'Pitt Meadows' => 'Pitt Meadows',
    'Port Coquitlam' => 'Port Coquitlam',
    'Port Moody' => 'Port Moody',
    'Richmond' => 'Richmond',
    'Surrey' => 'Surrey',
    'West Vancouver' => 'West Vancouver'
);

    $address_fields['city']['type'] = 'select';
    $address_fields['city']['options'] = $fields;

    return $address_fields;
}

Upvotes: 1

Views: 631

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29624

You add $fields['city']['options'] to $address_fields['city']['options'] = $fields; there is the mistake

// Billing and Shipping fields on my account edit-addresses and checkout
function custom_override_default_address_fields( $address_fields ) {

    $option_cities = array(
        '' => __( 'Select your city' ),
        'Burnaby' => 'Burnaby',
        'Coquitlam' => 'Coquitlam',
        'Langley' => 'Langley',
        'New Westminster' => 'New Westminster',
        'North Vancouver' => 'North Vancouver',
        'Pitt Meadows' => 'Pitt Meadows',
        'Port Coquitlam' => 'Port Coquitlam',
        'Port Moody' => 'Port Moody',
        'Richmond' => 'Richmond',
        'Surrey' => 'Surrey',
        'West Vancouver' => 'West Vancouver'
    );

    $address_fields['city']['type'] = 'select';
    $address_fields['city']['options'] = $option_cities;

    return $address_fields;
}
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

Upvotes: 1

Related Questions