Magento – How to create customer custom Drop Down attributes in magento 2

customer-attributedropdown-attribute

Namespace/Module/setup/installData.php

$customerSetup->addAttribute(Customer::ENTITY,'credit_type',
                [
                    'type' => 'int',
                    'label' => 'Credit Type requested',
                    'input' => 'select',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
                    'required' => false,
                    'default' => '0',
                    'sort_order' => 167,
                    'system' => false,
                    'position' => 167,
                    'adminhtml_only' => 1,
                    'option' =>
                        array (
                            'values' =>
                                array (
                                    271 => 'Credit Card',
                                    272 => 'Paypal',
                                    273 => 'Master Card',
                                    274 => 'Visa',
                                ),
                        ),
                ]
            );
            $myAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'credit_type');
            // more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
            $myAttribute->setData(
                'used_in_forms', ['adminhtml_customer', 'customer_account_create','wholesale_account_create', 'customer_account_edit']);
            $myAttribute->save();

My template.phtml file code:

       <div class="control">
            <select name="mySelect" id="myselect" title="<?= $block->escapeHtml(__('Requested Credit Type')) ?>" class="validate-select">
                <option disabled selected value> -- Please Select Credit Type Requested -- </option>
                <option value="271">Credit Card</option>
                <option value="272">Open Terms</option>
                <option value="273">Mega / NBA</option>
                <option value="274">Cantrex</option>
            </select>
        </div>

Data doesn't store in backend

Best Answer

Are you talking about Magento 2? But I see the path from Magento 1

Namespace/Module/sql/module_steup/installData.php

It’s not quite clear what you want, but I see that you’ve left out the attribute set and the customer group.

$myAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'credit_type');
$myAttribute->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create','wholesale_account_create', 'customer_account_edit']);
$attribute->addData([
                      'attribute_set_id' => 1,
                      'attribute_group_id' => 1
                    ]);
$myAttribute->save();

Remove the module from the setup_module table and install again.

Related Topic