I have a custom module app/code/Learning/FirstUnit/
and in that i have LayoutProcessor.php file in app/code/Learning/FirstUnit/Plugin
.
Please see the below code in LayoutProcessor.php
<?php
namespace Learning\FirstUnit\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" => false],
['value' => "test3", "label" => "Test city 3", "is_default" => false]
];
}
}
This code is for to make the city field in checkout page as dropdown.
In getCitiesDropdown function I need to get the id of user selected country and region name that user type or selected . How can I get this .
I have custom table in database, so if I get the user typed region name then I will load the corresponding city name in the dropdown form that table.
Please help.