1

I am trying to create a custom select type of customer attribute for admin only. When I am running my upgradeData script, Its running fine and attribute created in the database, but not showing in admin customer form.

But If I am creating a text field instead of a select field then that showing fine at admin customer form.

Here is my code -

namespace Mynamespace\Mymodule\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;
use Magento\Customer\Api\CustomerMetadataInterface;

class UpgradeData implements UpgradeDataInterface
{
    private $eavSetupFactory;
    protected $eavSetup;

    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig,EavSetup $eavSetup)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig       = $eavConfig;
        $this->eavSetup = $eavSetup;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.3', '<')){
            $this->eavSetup->removeAttribute(\Magento\Customer\Model\Customer::ENTITY, 'customer_reseller_id');

            $this->eavSetup->addAttribute(
                CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
                'customer_reseller_id',
                [   
                    'type'      => 'int',
                    'label'     => 'Reseller',
                    'input'     => 'select',
                    'backend'   => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                    'source'    => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'required'  => true,
                    'visible'   => true,
                    'user_defined' => true,
                    'position'  => 160,
                    'sort_order'=> 160,
                    'system'    => false
                ]
            );

            $customAttribute = $this->eavConfig->getAttribute(
                CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER,
                'customer_reseller_id'
            );

            $customAttribute->setData(
                'used_in_forms',
                ['adminhtml_customer']
            );

            $customAttribute->save();
        }
    }
}

Please let me know what I am doing wrong?

Thanks

2
[
    'type' => 'int',
    'label' => 'Reseller',
    'input' => 'select',
    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
    'required' => true,
    'default' => '0',
    'sort_order' => 160,
    'system' => false,
    'position' => 160,
    'adminhtml_only' => 1,
    'option' =>
         array (
            'values' =>
                 array (
                     0 => 'Custom val 1',
                     1 => 'Custom val 2',
                     2 => 'Custom val 3',
                 ),
         ),
]

Update

$customAttribute->setData(
                'used_in_forms',
                ['adminhtml_customer']
            );

With:

$customAttribute->setData(
                'used_in_forms',
                ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']
            );
| improve this answer | |
  • But I only need in admin customer form, then why I put the other form's name in the array. One more thing if this is the issue the why "text" type attribute showing in same way. – Atul Aug 17 '18 at 6:25
  • Adding 'adminhtml_customer', 'customer_account_create', 'customer_account_edit' in used_in_forms doesn't add the attribute in front automatically ! you need for that to add the field in phtml, Since you only want it on admin, so leave it like this. – PЯINCƏ Aug 17 '18 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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