7

Magento 2 - How to create a product attribute and assigned it all attribute set using setup script?

10

By default if you provide a value for 'group' in the attribute configuration array the attribute will be added to that group in all attribute sets:

$eavSetup->addAttribute('catalog_product', 'my_attribute', [
    ...
    'group' => 'General',  // or some other group name
    ...
]);
5

This might be helpful:-

Create InstallData.php in your Setup folder and paste this code in your file

<?php

namespace [Vendor]\[Module]\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;

class InstallData implements InstallDataInterface
{

    private $eavSetupFactory;

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

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

        /**
         * Add attributes to the eav/attribute
         */
        $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'background_image');
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'background_image',/* Custom Attribute Code */
            [

                'type' => 'varchar',/* Data type in which formate your value save in database*/
                'backend' => '',
                'frontend' => '',
                'label' => 'Background Image', /* lablel of your attribute*/
                'input' => 'media_image',
                'class' => '',
                'source' => '',
                                /* Source of your select type custom attribute options*/
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                                    /*Scope of your attribute */
                'visible' => false,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false
            ]
        );


        $entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
        $attributeSetIds = $eavSetup->getAllAttributeSetIds($entityTypeId);
        foreach ($attributeSetIds as $attributeSetId) {
            /**
             * getAttributeGroupId($entityTypeId, $attributeSetId, "Group_Code");
             * 
             */
            $groupId = $eavSetup->getAttributeGroupId($entityTypeId, $attributeSetId, "image-management");
            $eavSetup->addAttributeToGroup(
                $entityTypeId,
                $attributeSetId,
                $groupId,
                'background_image',
                null
            );
        }
    }
}
  • 1
    I found that if you are trying to add an attribute to the 'Images' group then this is the only way. Magento throws errors when you use the 'group' => '<group name>' method. – dmatthew May 18 '18 at 16:31
3

Here is the script by which can you add attribute to all attribute set.
Step 1:-Create InstallData.php in your Setup folder and paste this code in your file.

namespace Ktpl\BrandManagement\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;

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

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

    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
     public function install(ModuleDataSetupInterface $setup,     ModuleContextInterface $context)
    {
     /** @var EavSetup $eavSetup */
     $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

     /**
     * Add attributes to the eav/attribute
     */

    $eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
            'shop_by_brand',
            [
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'Brand',
                'input' => 'multiselect',
                'class' => '',
                'source' => '',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => 0,
                'searchable' => false,
                'filterable' => true,
                'comparable' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
     }
   }

Please let me know if you have any query about it. please remove extra space if require.

  • For perticular group to assign you need to add this line 'group' => '<group_name>' – Manish Goswami Oct 3 '16 at 13:39

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.