8

How to create customer attributes magento 2 so it works in running script without install script:-

    /**
     * @var   \Magento\Eav\Setup\EavSetupFactory
     */
    protected $eavEavSetupFactory;

    public function __construct(
        ...
        \Magento\Eav\Setup\EavSetupFactory $eavEavSetupFactory,
        ...     
    )
    {
        ...
        $this->eavEavSetupFactory = $eavEavSetupFactory;
        ...
    }

    public function createOrUpdateCustomerUDA($value, $input, $type)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->eavEavSetupFactory->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, $value, [
            'type' => $type,
            'label' => $value,
            'input' => $input,
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);
        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $value)
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

    $attribute->save();
    }

I am using this function in my custom module to create customer attributes, but it give me below error, can please guide me how to pass the $setup in this function so it's working fine:-

Parse error: syntax error, unexpected '$setup' (T_VARIABLE), expecting ',' or ')' in Customer.php on line 540
4
  • 1
    I think there is no need to pass ['setup' => $setup] to the factory. Just remove it and everything should be fine. Commented Jan 3, 2018 at 9:48
  • After remove ['setup' => $setup] get below error:- Uncaught TypeError: Argument 1 passed to Magento\Setup\Module\DataSetup::__construct() Commented Jan 5, 2018 at 14:41
  • Magento\Framework\Setup\ModuleDataSetupInterface Can you try to load this in construct and pass its reference as argument for setup argument Commented Jan 12, 2018 at 7:20
  • @Pankaj after set the construct dependency get error:- Uncaught TypeError: Argument 1 passed to Magento\Setup\Module\DataSetup::__construct() Commented Jan 12, 2018 at 9:49

2 Answers 2

5

After a long time and debugging and spent time on reading blogging site/forum I found the solution to create attribute in magento to programmatically and able to save value in this custom created attributes.

Here is mu complete code that works in my custom module:-

<?php
namespace Company\Mymodule\Helper;

use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Eav\Setup\EavSetupFactory
     */
    protected $eavEavSetupFactory;
    protected $eavConfig;
    protected $attributeSetFactory;

    public function __construct(
        ...
        \Magento\Eav\Setup\EavSetupFactory $eavEavSetupFactory,
        \Magento\Eav\Model\Config $eavConfig,       
        AttributeSetFactory $attributeSetFactory
        ...
    )
    {
        ...
        $this->eavEavSetupFactory = $eavEavSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
        $this->eavConfig = $eavConfig;
        ...
    }   

    protected function createOrUpdateCustomerUDA($value, $input, $type)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->eavEavSetupFactory->create();

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

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

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode, [
            'type' => $type,
            'label' => $value,
            'input' => $input,
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 90,
            'position' => 999,
            'system' => 0,
        ]);

        $attribute = $this->eavConfig->getAttribute(\Magento\Customer\Model\Customer::ENTITY, $attributeCode)
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],
            ]);

        $attribute->save();

        unset($customerSetup);      
    }

    protected function getAttributeCode($str)
    {
        return "pre_" . strtolower(str_replace(" ", "", $str));    
    } 
}

Please use above code and let me know if any issue. Happy coding :)

1
  • hi @Purushotam, can you provide the code to create product attribute as well please? Commented Jan 8, 2019 at 5:47
3

Try following code:

  1. app/code/[VendorName]/[ModuleName]/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[VendorName]_[ModuleName]',
    __DIR__
);
  1. app/code/[VendorName]/[ModuleName]/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[VendorName]_[ModuleName]" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>
  1. app/code/[VendorName]/[ModuleName]/Setup/InstallData.php
namespace [VendorName]\[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;

class InstallData implements InstallDataInterface
{
    protected $customerSetupFactory;

    private $attributeSetFactory;

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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

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

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'custom_username', [
            'type' => 'varchar',
            'label' => 'Custom Field',
            'input' => 'text',
            'user_defined' => false,
            'required' => false,
            'visible' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);

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

        $attribute->save();
    }
}
1
  • I already find this but this is using the install script. But I want to use without install script Commented Dec 26, 2017 at 2:54

Your Answer

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

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