🔖 Magento 2 Attributes & Attribute Sets – Customize Your Product Data
In Magento 2, Attributes define product data (like color, size, material), and Attribute Sets group these attributes together. Think of them as custom fields that make your product catalog more flexible and powerful.
🔢 What is a Product Attribute?
A product attribute is a field that holds specific data about a product — such as Color, Size, Material, or Brand. Magento allows you to create custom attributes to collect any type of information.
- Attribute Code: Internal identifier (e.g.
custom_color
) - Frontend Label: Display name (e.g.
Color
) - Input Types: Text field, dropdown, multiple select, etc.
📦 What is an Attribute Set?
An Attribute Set is a collection of attributes assigned to a product type. For example:
- Clothing Set: Color, Size, Fabric
- Electronics Set: Brand, Warranty, Voltage
When creating a product, you choose an attribute set — this determines which fields show up in the product form.
⚙️ How to Create a New Attribute (Admin UI)
- Go to Stores > Attributes > Product
- Click Add New Attribute
- Fill in required fields (code, label, input type)
- Save and assign to an attribute set
💻 Creating Product Attribute via InstallData Script
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 { protected $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, 'custom_attribute', [ 'type' => 'varchar', 'backend' => '', 'frontend' => '', 'label' => 'Custom Attribute', 'input' => 'text', 'class' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => true, 'default' => '', 'searchable' => true, 'filterable' => true, 'comparable' => true, 'visible_on_front' => true, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => '' ] ); } }
🧩 Assigning Attributes to Attribute Sets
- Go to Stores > Attributes > Attribute Set
- Select an attribute set (e.g., Default)
- Drag your custom attribute from the Unassigned Attributes list to a group
- Save the attribute set
🧠 Summary
- Attributes = Custom product data fields
- Attribute Sets = Group of attributes for specific product types
- Essential for filtering, searching, and customizing catalog UX
By mastering Attributes and Attribute Sets, you gain full control over your catalog’s structure and user experience. 🧙♂️