An attribute is a property of products that can be used in various Magento 2 store’s function.
While creating extensions, developers may have the requirement to create product, category and CMS page attributes in Magento 2. Also, the product attributes help visitors find the most suitable products according to their requirements.
Magento 2 has predefined attributes like Name, Price, and Description. However, to improve the search functionality and allow the users to compare the products, use the custom product attributes!
Method to Create Product, Category and CMS Page Attributes in Magento 2:
Place the below code at [Vendor]\[Module]\Setup\InstallData.php
<?php namespace [Vendor]\[Module]\Setup; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Setup\EavSetupFactory; use Magento\Catalog\Model\Product; use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; use Magento\Catalog\Model\Category; use Magento\Framework\DB\Ddl\Table; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $eavSetup = $this->eavSetupFactory->create();
To Create Product Attribute:
$eavSetup->addAttribute( Product::ENTITY, 'attribute_name', [ 'group' => 'group_name', 'type' => 'text', 'label' => 'Label', 'input' => 'text', 'required' => false, // true if required 'global' => ScopedAttributeInterface::SCOPE_STORE, 'visible' => true, 'visible_on_front' => true ] );
Check another method to create product attribute from our blog. After done with this you can create custom order in Magento 2 which will be later seen in admin grid and will be helpful for your business in managing and fulfill the online orders efficiently.
To Create Category Attribute:
$eavSetup->addAttribute( Category::ENTITY, 'attribute_name', [ 'group' => 'group_name', 'type' => 'text', // varchar,etc.. 'label' => 'Label', 'input' => 'text', 'required' => false, 'global' => ScopedAttributeInterface::SCOPE_STORE, 'visible' => true, 'visible_on_front' => true ] );
To Create CMS Page Attribute:
$setup->getConnection()->addColumn( $setup->getTable('cms_page'), 'attribute_name', [ 'type' => Table::TYPE_TEXT, // assign type as you need 'length' => '255', 'nullable' => true, // if required false 'comment' => 'comment text' ] ); $installer->endSetup(); } }
Method to Remove Product or Category Attribute:
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->removeAttribute( \Magento\Catalog\Model\Product::ENTITY, 'attribute_name'); $eavSetup->removeAttribute( \Magento\Catalog\Model\Category::ENTITY, 'attribute_name'); }
Display the products more attractively with the help of the custom attributes that can be easily created with the help of the above method! Likewise you can also programmatically set product position in a category in Magento 2 as the product position value determines the order of products on the category page.
Thank you.