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,
]
Solved! Go to Solution.
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.
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"
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.
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();
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.
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.