0

I have written the following script to create a custom customer attribute in Magento 2.2.3. When I run setup:upgrade magento tells me data was installed but the attribute does not appear in eav_attribute table.

<?php

namespace MyNameSpace\StoreName\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\Config;
use Magento\Customer\Model\Customer;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'store_real_name',
            [
            'type'         => 'varchar',
            'label'        => 'Store Name',
            'input'        => 'text',
            'required'     => false,
            'visible'      => true,
            'user_defined' => true,
            'position'     => 999,
            'system'       => 0,
            ]
        );
        $storeName = $this->eavConfig->getAttribute(Customer::ENTITY, 'store_real_name');


        $storeName->setData(
        'used_in_forms',
        ['adminhtml_customer', 'customer_account_edit']

        );
        $storeName->save();
    }
}

I have used an identical script to create other attributes without issue, but for some reason this one won't save. I have tried to disable and enable the module and re-run setup:upgrade but nothing seems to work.

  • Your script looks Ok, please try to execuse: php bin/magento setup:upgrade, php bin/magento setup:static-content:deploy – PЯINCƏ Jul 25 '18 at 8:36
0

Use This code it will create Customer Attribute

<?php
namespace MyNameSpace\StoreName\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;
    }


    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, 'store_real_name', [
            'type' => 'varchar',
            'label' => 'Store Name',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'position' =>999,
            'system' => 0,
        ]);

        $storeName = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'store_real_name')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer', 'customer_address_edit']
        ]);

        $storeName->save();

    }
}
  • Thanks for the response but this didn't work either, exactly same result as original post. I've checked the usual logs to see if there's any issues but cannot see anything. – matt Jul 25 '18 at 10:48
0

Try to run following commands after do major changes in your code. Example if you create script than you might run upgrade command.

Commands

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

php bin/magento cache:clean or php bin/magento cache:flush

The above commands should be run in console.

  • Yes, run all of those commands, still nothing. – matt Jul 25 '18 at 10:49

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.