You need to customize your checkout page layout processor. Try the below codes for creating a new module to customize the checkout page fields:
Step 1:
Create a registration.php file for your module:
File path: app/code/MyVendor/MyModule/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::Register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MyVendor_MyModule',
__DIR__
);
Step 2:
Create a module.xml file for your module:
File path: app/code/MyVendor/MyModule/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MyVendor_MyModule" setup_version="1.0.0"/>
</config>
Step 3:
Declare a plugin for the checkout layout processor.
File path: app/code/MyVendor/MyModule/etc/frontend/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="customised_layout_processor" type="MyVendor\MyModule\Plugin\LayoutProcessor"/>
</type>
</config>
Step 4:
Create plugin class:
File path: app/code/MyVendor/MyModule/Plugin/LayoutProcessor
<?php
namespace MyVendor\MyModule\Plugin;
use Magento\Directory\Helper\Data as DirectoryHelper;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
use Magento\Store\Model\StoreManagerInterface;
use function PHPUnit\Framework\assertIsCallable;
class LayoutProcessor
{
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
$result
) {
//For shipping form
$result['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['city'] = [
'component' => 'Magento_Ui/js/form/element/select',
'config' => [
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/select',
'id' => 'drop-down',
],
'dataScope' => 'shippingAddress.city',
'label' => __('City'),
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => [],
'sortOrder' => 70,
'id' => 'drop-down',
'options' => $this->getCitiesDropdown()
];
//For Billing Form
foreach ($result['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['payments-list']['children'] as $key => $payment) {
if (isset($payment['children']['form-fields']['children']['city'])) {
$result['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']['payments-list']['children'][$key]['children']['form-fields']['children']
['city'] = [
'component' => 'Magento_Ui/js/form/element/select',
'config' => [
'customScope' => 'shippingAddress',
'template' => 'ui/form/field',
'elementTmpl' => 'ui/form/element/select',
'id' => 'drop-down',
],
'label' => __('City'),
'provider' => 'checkoutProvider',
'visible' => true,
'validation' => [],
'sortOrder' => 70,
'id' => 'drop-down',
'options' => $this->getCitiesDropdown()
];
}
}
return $result;
}
public function getCitiesDropdown()
{
return [['value' => "test1", "label" => "Test city 1", "is_default" => true], ['value' => "test2", "label" => "Test city 2", "is_default" => true]];
}
}
Step 5:
Run the below commands:
bin/magento module:enable MyVendor_MyModule
bin/magento setup:upgrade
bin/magento s:d:c
bin/magento cache:flush
After following these 5 steps your module will get enable. Now you can check your checkout page for output:
You will get the results like the below screenshot:
Hope it will help you!