0

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.

1
  • I hope that my answer helped to you to add multiple field by same InstallData.php.. If its really helpful then please accept the answer. So it will be helpful for others also.. Commented Apr 7, 2018 at 5:38

2 Answers 2

0

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.

0

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
3
  • can i add attribute using UpgradeSchema.php or InstallSchema.php file? Commented Apr 6, 2018 at 10:18
  • @BhavishaThaker, Above code is from installData.php, You can use it UpgradeSchema but you need to modify as per your need. Happy Coding !!
    – anonymous
    Commented Apr 6, 2018 at 10:20
  • If it helps you than give upvote and accept answer to help others. :)
    – anonymous
    Commented Apr 6, 2018 at 10:32

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.