3

How can I achieve State and Country field functionality in Admin under customer Account Information?

I have referred this link to create in front-end

https://magecomp.com/blog/create-country-state-dropdown-in-magento-2-custom-frontend-form/

In customer Registration form got the functionality.

enter image description here

I did a small change in custom attribute I used Magento\Catalog\Model\Product\Attribute\Source\Countryofmanufacture source to list the country. For state I refereed the above link.

In Admin I don't know how to achieve state functionality.

enter image description here

I need to achieve state list if options are available and text field if not.

Can someone help me with this I referred lost of blog and Stack Exchange but can't find a single post that explains the workflow to achieve in admin.

1
  • Need Help on this :(
    – fernandus
    Oct 7, 2020 at 12:26

2 Answers 2

0

app/code/Vendor/Module/view/adminhtml/ui_component/customer_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <field name="country_id">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Directory\Model\Config\Source\Country</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Country</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">store</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/form/element/country</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="region">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Directory\Model\ResourceModel\Region\Collection</item>
                <item name="config" xsi:type="array">
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">State/Region</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="source" xsi:type="string">store</item>
                    <item name="customEntry" xsi:type="string">region</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                    </item>
                    <item name="filterBy" xsi:type="array">
                        <item name="target" xsi:type="string">${ $.provider }:${ $.parentScope }.country_id</item>
                        <item name="field" xsi:type="string">country_id</item>
                    </item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

Let me know if you facing any issues.

Thanks.

0

To add state and country attributes for customers in the Magento 2 admin panel, you can follow these steps:

Create a custom module: If you don't have a custom module already, create one. This module will be used to add the necessary code and configurations.

Define the attribute: Within your custom module, create a new file app/code/Vendor/ModuleSetup/InstallData.php (replace "Vendor" and "Module" with your module's namespace and name). In this file, you'll define the attribute and its properties. Here's an example code snippet:

<?php
namespace Vendor\ModuleSetup;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $customerSetupFactory;
    private $attributeSetFactory;

    public function __construct(
        CustomerSetupFactory $customerSetupFactory,
        AttributeSetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(
            Customer::ENTITY,
            'state',
            [
                'type' => 'varchar',
                'label' => 'State',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'position' => 333,
                'system' => 0
            ]
        );

        $customerSetup->addAttribute(
            Customer::ENTITY,
            'country',
            [
                'type' => 'varchar',
                'label' => 'Country',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'position' => 334,
                'system' => 0
            ]
        );

        $attributeSetId = $customerSetup->getDefaultAttributeSetId();

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttributeToSet(
            Customer::ENTITY,
            $attributeSetId,
            $attributeGroupId,
            'state'
        );

        $customerSetup->addAttributeToSet(
            Customer::ENTITY,
            $attributeSetId,
            $attributeGroupId,
            'country'
        );

        $setup->endSetup();
    }
}

enter code here

This code snippet creates two attributes: state and country, and adds them to the default attribute set for customers.

Run the setup and upgrade commands: Once you have defined the attributes, you need to run the following commands from your Magento root directory:

php bin/magento setup:upgrade
php bin/magento cache:clean

These commands will update the database schema and clear the cache.

After following these steps, you should be able to see the state and country attributes in the customer details section of the Magento.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.