Magento – Magento2 : How to add city field to dropdown on checkout page

magento2shipping-methods

I want add city drop-down in checkout page without any extension. I have tried some reference link for adding city dropdown but it is adding ony on cart page on estimate section.
I want to add in checkout page. kindly help me if anyone have an idea for that.

Best Answer

Here are the few free extensions for Magento 2 city Dropdwon

https://packagist.org/packages/php-cuong/magento2-city-dropdown

https://github.com/EaDesgin/Magento2-City-Dropdown

If you are not willing to use any extension then you will need to overwrite this file vendor\magento\module-checkout\Block\Cart\LayoutProcessor.php and replace the process function with this one. Option array is empty in below code

public function process($jsLayout)
{
    $elements = [
        'city' => [
        'visible' => true,
        'formElement' => 'select',
        'label' => __('City'),
        'value' =>  '',
        'options' => array_option(),
        ],
        'country_id' => [
            'visible' => true,
            'formElement' => 'select',
            'label' => __('Country'),
            'options' => [],
            'value' => null
        ],
        'region_id' => [
            'visible' => true,
            'formElement' => 'select',
            'label' => __('State/Province'),
            'options' => [],
            'value' => null
        ],
        'postcode' => [
            'visible' => true,
            'formElement' => 'input',
            'label' => __('Zip/Postal Code'),
            'value' => null
        ]
    ];

    if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
        $jsLayout['components']['checkoutProvider']['dictionaries'] = [
            'country_id' => $this->countryCollection->loadByStore()->setForegroundCountries(
                $this->topDestinationCountries->getTopDestinations()
            )->toOptionArray(),
            'region_id' => $this->regionCollection->addAllowedCountriesFilter()->toOptionArray(),
        ];
    }
    if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
        ['address-fieldsets']['children'])
    ) {
        $fieldSetPointer = &$jsLayout['components']['block-summary']['children']['block-shipping']
        ['children']['address-fieldsets']['children'];
        $fieldSetPointer = $this->merger->merge($elements, 'checkoutProvider', 'shippingAddress', $fieldSetPointer);
        $fieldSetPointer['region_id']['config']['skipValidation'] = true;
    }
    return $jsLayout;
}

There should be custom option array for cities in array options

I hope this will help

Related Topic