How to get coustomer Country List option in ui component form with multiselect

How to get coustomer Country List option in ui component form with multiselect


Hi Magento friend today's i'm share How to get coustomer Country List option in ui component form 

In Uicomponent create a field - 

How we are Integrate the coustomer Email Country options in uicomponent Form - 


<field name="customer_country" formElement="select">

      <argument name="data" xsi:type="array">

        <item name="options" xsi:type="object">vendor_name\module_name\Model\Options\CustomerCountry</item>

        <item name="config" xsi:type="array">

          <item name="dataType" xsi:type="string">text</item>

          <item name="label" translate="true" xsi:type="string">Select Country</item>

          <item name="formElement" xsi:type="string">multiselect</item>

          <item name="dataScope" xsi:type="string">customer_country</item>

          <item name="validation" xsi:type="array">

            <item name="required-entry" xsi:type="boolean">true</item>

         </item>

        </item>

      </argument>

    </field> 


Create a CustomerCountry.php file in vendor_name\module_name\Model\Options\


<?php

namespace vendor_name\module_name\Model\Options;

use Magento\Framework\Option\ArrayInterface;

class CustomerCountry implements ArrayInterface {

    protected $_customers;

    protected $_countryFactory;

    public function __construct(\Magento\Customer\Model\Customer $customers, \Magento\Directory\Model\CountryFactory $countryFactory) {

        $this->_customers = $customers;

        $this->_countryFactory = $countryFactory;

    }

    public function toOptionArray() {

        $customersdata = $this->_customers->getCollection()->addAttributeToSelect("*")->load();

        foreach ($customersdata as $data) {

            $customerAddress = $data->getAddresses();

            foreach ($customerAddress as $cdata) {

                $countrycode = $cdata->getCountryId();

                $fullCountryName = $this->getCountryName($countrycode);

                $this->_options[] = ['label' => $fullCountryName, 'value' => $countrycode];

            }

        }

        $uniqueCountry = array_map("unserialize", array_unique(array_map("serialize", $this->_options)));

        return $uniqueCountry;

    }

    public function getCountryName($countryCode) {

        $country = $this->_countryFactory->create()->loadByCode($countryCode);

        return $country->getName();

    }

}


How to get coustomer Country List option in ui component form with multiselect





Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post