Question

How can I manage my InstallData.php file when I want to create multiple attribute for customer and product?

Here, in my InstallData file I have create an attribute for the product.

    namespace Backadmin\Grid\Setup;

    use Magento\Framework\Setup\InstallDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;

    /**
    * @codeCoverageIgnore
    */
    class InstallData implements InstallDataInterface
    {
        /**
         * Eav setup factory
         * @var EavSetupFactory
         */
        private $eavSetupFactory;

        /**
         * Init
         * @param CategorySetupFactory $categorySetupFactory
         */
        public function __construct(\Magento\Eav\Setup\EavSetupFactory $eavSetupFactory)
        {
            $this->eavSetupFactory = $eavSetupFactory;
        }

        /**
         * {@inheritdoc}
         * @SuppressWarnings(PHPMD.CyclomaticComplexity)
         * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
         * @SuppressWarnings(PHPMD.NPathComplexity)
         */
        public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
        {
            $eavSetup = $this->eavSetupFactory->create();
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'clothing_material',
                [
                    'group' => 'General',
                    'type' => 'varchar',
                    'label' => 'Clothing Material',
                    'input' => 'text',
                    'source' => '',
                    'frontend' => '',
                    'backend' => '',
                    'required' => false,
                    'sort_order' => 50,
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'is_used_in_grid' => false,
                    'is_visible_in_grid' => false,
                    'is_filterable_in_grid' => false,
                    'visible' => true,
                    'is_html_allowed_on_front' => true,
                    'visible_on_front' => true
                ]
            );
        }

}

Now I want to create an attribute for the customer so how can I manage InstallData file.

For example, I have removed this and put the customer attribute code? otherwise, I will create a new file for this?

Please, suggest me how to manage this structure in single file?

If you guys have the best link for this how to manage multiple attributes so please share with me.

Was it helpful?

Solution

You can manage customer attribute, product attribute and category attribute in same InstallData.php

Please follow this structure in your file.

<?php

namespace Backadmin\Grid\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
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;

    private $eavSetupFactory;

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

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /* Create Customer Attribute */
        $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, 'allow_customer', [
            'type' => 'int',
            'label' => 'Customer Allowed',
            'input' => 'select',
            'required' => true,
            'visible' => true,
            'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
            'user_defined' => true,
            'sort_order' => 300,
            'position' => 400,
            'default' => 1,
            'system' => false,
            'is_used_in_grid' => 1,
            'is_visible_in_grid' => 1,
            'is_filterable_in_grid' => 1,
            'is_searchable_in_grid' => 1,
        ]);
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'is_builder_account')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);
        $attribute->save();

         /* Create Product Attribute */
        $productSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $productSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'clothing_material',
            [
                'group' => 'General',
                'type' => 'varchar',
                'label' => 'Clothing Material',
                'input' => 'text',
                'source' => '',
                'frontend' => '',
                'backend' => '',
                'required' => false,
                'sort_order' => 50,
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'is_used_in_grid' => false,
                'is_visible_in_grid' => false,
                'is_filterable_in_grid' => false,
                'visible' => true,
                'is_html_allowed_on_front' => true,
                'visible_on_front' => true
            ]
        );
    }

}

Same as the above structure you can also create an attribute for category also.

I hope this will help to you.

OTHER TIPS

You don't need to create multiple files for different attributes.

Check my code its working for me. I've created Product and Customer attributes.

<?php
    namespace Vendor\Module\Setup;

     use Magento\Framework\Module\Setup\Migration;
     use Magento\Framework\Setup\InstallDataInterface;
     use Magento\Framework\Setup\ModuleContextInterface;
     use Magento\Framework\Setup\ModuleDataSetupInterface;
     use Magento\Customer\Model\GroupFactory;
     use Magento\Eav\Setup\EavSetupFactory;
     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;



    class InstallData implements InstallDataInterface
   {
     protected $groupFactory;
/**
 * @var CustomerSetupFactory
 */
protected $customerSetupFactory;

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

private $eavSetupFactory;

/**
 * @param CustomerSetupFactory $customerSetupFactory
 * @param AttributeSetFactory $attributeSetFactory
 */

/**
 * I'd like a customer group factory please Sir!
 */
public function __construct(GroupFactory $groupFactory,EavSetupFactory $eavSetupFactory,CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory) {

    $this->groupFactory = $groupFactory;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
    $this->eavSetupFactory = $eavSetupFactory; 
}

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

    $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'brands');

    $eavSetup->addAttribute(
             \Magento\Catalog\Model\Product::ENTITY, 'brands', [
            'type' => 'text',
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'frontend' => '',
            'label' => 'Brands',
            'input' => 'select',
            'group' => 'General',
            'class' => 'brands',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' =>    true,
            'required' => true,
            'user_defined' => true,
            'default' => '1',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false
                ]
    );

    $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
    $setup->startSetup();
    $attributesInfo = [
        'customer_avatar' => [
            'label' => 'Customer Avatar',
            'type' => 'varchar',
            'input' => 'file',
            'position' => 1000,
            'visible' => true,
            'required' => false,
            'system' => 0,
            'user_defined' => true,
            'position' => 1000,
        ]
    ];
    $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();
    /** @var $attributeSet AttributeSet */
    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
    foreach ($attributesInfo as $attributeCode => $attributeParams) {
        $customerSetup->addAttribute(Customer::ENTITY, $attributeCode, $attributeParams);
    }
    $magentoUsernameAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'customer_avatar');
    $magentoUsernameAttribute->addData([
        'attribute_set_id' => $attributeSetId,
        'attribute_group_id' => $attributeGroupId,
        'used_in_forms' => ['adminhtml_customer','customer_account_edit','adminhtml_checkout','adminhtml_customer_address','customer_address_edit','customer_register_address'],
    ]);
    $magentoUsernameAttribute->save();

        $setup->endSetup();
}

}
?>

Note:- Remove your existing module entry from setup_module table if you are creating in existing module. Execute below commands after add above code.

php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top