Create Product Attribute
In Magento 2, product attributes are used to define characteristics of a product such as color, size, and material. Creating product attributes is an important part of store setup as it allows customers to filter, search and compare products on the basis of different attributes.
Syntax
To create a new product attribute in Magento 2, the following syntax is used:
$installer = $setup;
$installer->startSetup();
$eavSetup = new \Magento\Eav\Setup\EavSetup($installer);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'attribute_code',
[
'type' => 'int',
'label' => 'Attribute Label',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => false,
'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
'used_in_product_listing' => true,
'unique' => false,
]
);
$installer->endSetup();
Example
To create a new product attribute in Magento 2 for size with a code of ‘size’, the following code can be used:
$installer = $setup;
$installer->startSetup();
$eavSetup = new \Magento\Eav\Setup\EavSetup($installer);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'size',
[
'type' => 'varchar',
'label' => 'Size',
'input' => 'select',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
'required' => true,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => 'simple,configurable,virtual,bundle,downloadable',
'option' => [
'values' => [
'Small',
'Medium',
'Large'
]
]
]
);
$installer->endSetup();
Output
The above example creates a new product attribute for ‘size’ which is a select type with three options - ‘Small’, ‘Medium’, and ‘Large’. This attribute will be applicable to simple, configurable, virtual, bundle and downloadable products.
Explanation
The above code adds a new product attribute using addAttribute() function of EavSetup object. The function uses a set of parameters to create the attribute including:
type
: specifies the attribute’s data type. This parameter is required and can takevarchar
,int
,float
,decimal
, andtext
.label
: specifies the label of the attribute which is displayed in the store.input
: specifies the input type of the attribute. This parameter is required and can taketext
,textarea
,price
,media_image
,multiselect
, andselect
.source
: specifies the source of attribute options. This parameter is required whenselect
ormultiselect
is used as the input type.required
: specifies whether the attribute is required or not.global
: specifies the scope of the attribute. This parameter determines the scope as eitherSCOPE_STORE
,SCOPE_GLOBAL
, orSCOPE_WEBSITE
.used_in_product_listing
: specifies whether the attribute can be used in product listings or not.unique
: specifies whether the attribute value should be unique or not.
Use
Creating new product attributes in Magento 2 is necessary for store setup as it allows customers to search, filter, and compare products based on different attributes. Store owners can create various product attributes like size, color, material, type, etc. depending on their product catalog.
Important Points
- Creating product attributes in Magento 2 requires the
EavSetup
object and its associated functions. - Product attributes can be created with different options such as label, input type, source of attribute options, required, scope, etc.
- Multiple product attributes can be created at the same time using an array of attribute codes and their respective options.
Summary
Creating a product attribute in Magento 2 is essential for store setup as it allows customers to filter, search and compare products based on characteristics like size, color, and material. Product attributes can be easily created using the EavSetup
object and its associated functions. Magento 2 provides various options for product attributes such as data type, input type, source of attribute options, scope, and more.