#StackBounty: #magento2 #module #layout #plugin #app Get Selected country id and region name in class LayoutProcessor of cusom module

Bounty: 50

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 LearningFirstUnitPlugin;

use MagentoDirectoryHelperData as DirectoryHelper;
use MagentoDirectoryModelResourceModelCountryCollectionFactory;
use MagentoStoreModelStoreManagerInterface;
use function PHPUnitFrameworkassertIsCallable;

class LayoutProcessor
{

    public function afterProcess(
        MagentoCheckoutBlockCheckoutLayoutProcessor $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.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.