2

I have added custom attribute of customer, It have added to database but not show in backend.

I have used

  • php bin/magento setup:upgrade

  • php bin/magento setup:static-content:deploy

  • php bin/magento cache:flush –all

    But still not show.I have added below code: enter image description here

Please help me!

4
  • where it does not showing?
    – Amit Bera
    Jan 18, 2018 at 10:31
  • please confirm your module Onlinebiz_CustomerTrade has been enabled or not from config.php (or else) you check in db setup_module table also? Jan 18, 2018 at 10:41
  • It not show in customer edit prnt.sc/i2494v . I have checked in db have had version prnt.sc/i249yh
    – jordan
    Jan 18, 2018 at 10:59
  • Hi jordan do not show your extensions file path everyone notify what is your work. Hide them if important !!!!!! Jan 18, 2018 at 11:02

1 Answer 1

3

You can try this :

namespace Vendor\ModuleName\Setup;

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

/**
 * @codeCoverageIgnore
 */
class InstallData implements InstallDataInterface
{

    /**
     * @var CustomerSetupFactory
     */
    protected $customerSetupFactory;

    /**
     * @var AttributeSetFactory
     */
    private $attributeSetFactory;

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


    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'attribute_code', [
            'type' => 'static',
            'label' => 'Label Name',
            'input' => 'select',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
            'source' => \Magento\Eav\Model\Entity\Attribute\Source\Table::class,
            'option' => ['values' => ['Text1', 'Text2','Text3','Text4','Text5']],
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'attribute_code')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => [
                    'customer_account_create',
                    'adminhtml_checkout',
                    'adminhtml_customer',
                    'customer_account_edit'
                ],
            ]);

        $attribute->save();
    }
}

You can edit 'used_in_forms' info according to your need . May be you dont'n need first option in the array .

3
  • Thank you help me! I will try it!
    – jordan
    Jan 18, 2018 at 11:18
  • Hi Shashank Gupta,I have added your code. It has show but When save, It not save in db.
    – jordan
    Jan 18, 2018 at 12:17
  • perfect answer!!! +1 :) made my day Sep 14, 2018 at 11:18

Your Answer

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

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