Create Custom Product Attributes and Groups in Magento 2

created July 19, 2019, last updated July 19, 2019.

.

Whilst migrating from Magento 1 to Magento 2 you might want to quickly recreate product attributes in Magento 2 programatically. Adding a lot of product attributes manually can be very time consuming so using the setup script in one of your modules let’s you quickly create new attributes.

An example of the install method is shown below with code for a confiurable product dropdown attribute and a text attribute.

To run the install script use php bin/magento setup:upgrade

To force the install script to run again you can delete the module entry in the setup_module database table and run setup:upgrade again. I guess a cleverer way would be to use the upgrade feature but this is really just intended as a one off process to perhaps install 50 product attributes that would otherwise be time consuming to manually configure.

Note the group name let’s you add these custom attributes into their own group which makes them more manageable in the admin interface.

<?php namespace GaiterJones\Catalog\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;
    }

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

        // attribute for configurable product
        //
        //
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'product_colour_x',
            [
                'type' => 'int',
                'group' => 'GAITERJONES Product',
                'backend' => '',
                'frontend' => '',
                'label' => 'product_colour_x',
                'default' => '',
                'input' => 'select',
                'class' => '',
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'searchable' => false,
                'filterable' => true,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false,
                'apply_to' => ''
            ]
        );

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'selling_unit',
            [
                'type' => 'varchar',
                'group' => 'GAITERJONES Product',
                'backend' => '',
                'frontend' => '',
                'label' => 'Selling Unit',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => 'Stück',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => false,
                'unique' => false,
                'apply_to' => ''
            ]
        );

     }
}

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.