cancel
Showing results for 
Search instead for 
Did you mean: 

add country dropdown to customer attribute

SOLVED

add country dropdown to customer attribute

hi everyone, I would like to have some information, or if there is any documentation, on how to add a custom dropdowns attribute for the customer that shows the list of countries. I suppose I have to use the Model Magento \ Directory \ Model \ Config \ Source \ Country somehow, but I haven't found the documentation about it.
Thanks so much.

 

'country_attribute',
[
'type' => 'varchar',
'label' => 'Country',
'input' => 'text',
'required' => true,
'visible' => true,
'user_defined' => true,
'position' => 999,
'system' => 0,
]

1 ACCEPTED SOLUTION

Accepted Solutions

Re: add country dropdown to customer attribute

the problem was only the user_defined value, if set to false the country list is preloaded correctly, otherwise the Model indicated for the source parameter is ignored.

 

 

View solution in original post

6 REPLIES 6

Re: add country dropdown to customer attribute

Hi @tunnel_dev 

 

you can try this

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

 

------------------------------------------------
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"

Re: add country dropdown to customer attribute

Hello @tunnel_dev 

 

Please try the below solution:

 

$customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'country_attribute',
    [
        'type' => 'varchar',
        'label' => 'Country',
        'input' => 'select',
        'required' => true,
        'visible' => true,
        'user_defined' => true,
        'position' => 999,
        'system' => 0,
        'source' => 'Magento\Directory\Model\Config\Source\Country',
    ]

I hope it helps.

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: add country dropdown to customer attribute

Thanks for the replies.

the attribute appears correctly as dropdown, but from the admin the options are empty, it does not automatically load the list of countries. Here is the code used. Maybe I need to change something in the $ attribute = $ this-> eavConfig-> getAttribute (Customer :: ENTITY, 'country_attribute'); statement?

Thanks a lot

 

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'country_attribute',
            [
                'type' => 'varchar',
                'label' => 'Custom Country',
                'input' => 'select',
                'required' => true,
                'visible' => true,
                'user_defined' => true,
                'position' => 999,
                'system' => 0,
                'source' => 'Magento\Directory\Model\Config\Source\Country',
            ]
        );
        $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'country_attribute');
        $attribute->setData(
            'used_in_forms',
            ['adminhtml_customer','customer_account_edit','customer_account_create']

        );
        $attribute->save();

Re: add country dropdown to customer attribute

Hi @tunnel_dev,

 

Please follow these 4 steps to create your own attributes for multi-purpose easily:

 

Step 1: Create InstallData.php file

Step 2: Modify data input for each kind of attribute

Step 3: [Optional] Customize layout

Step 4: Enable your module and see the result

 

Kindly refer to this article for more information. 

 

Hope this can help you! Let me know if you need further assistance. 

___________

If issue solved, Click Kudos & Accept as Solution.

LitExtension - #1 Shopping Cart Migration Expert

LitExtension helps store owners and agencies migrate all important data from one eCommerce platform to another accurately, securely and at the highest speed.

Visit website: http://litextension.com/

Re: add country dropdown to customer attribute

Hi Marcus,

I have already followed that tutorial, the dropdown field is created correctly, but although I have set the source with the model which therefore already contains the getAllOptions () method, the dropdown is empty, the list of Countries is not preloaded.

 

I set this source:

'source' => 'Magento\Customer\Model\ResourceModel\Address\Attribute\Source\Country'

 

I don't understand what the missing detail is.

Thanks al lot.

Re: add country dropdown to customer attribute

the problem was only the user_defined value, if set to false the country list is preloaded correctly, otherwise the Model indicated for the source parameter is ignored.