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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?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