Magento 2: Add Product Attribute Programmatically
In eCommerce, a trivial thing such as adding product attributes can help you attract and retail customers. How? By adding relevant and detailed product attributes, you can communicate vital information to the audience. Suppose you are an online gym apparel store in Dubai.
A search for ‘gym clothes Dubai’ leads to 8,620,000 results as you can see below.
Source: Google Search
A potential customer does not have the time to go through all these results and then compare the products. Instead, they will open first 4-6 links and assess whether the products meet their requirement. How can you ensure that your store coverts this potential lead to a customer?
By adding attributes such as quality and fabric type, you are providing additional information. It makes it easier to decide if the product is according to their needs. For example, the individual may be looking for cotton gym trousers. All five store may be offering cotton trousers, but this is not mentioned anywhere.
In today’s fast-paced environment, the customer will not call or message the store to confirm the fabric type. When your store is the only one mentioning the fabric type, the customer will naturally prefer to shop at your store. This is how the effective use of product attributes can come in handy.
Check Out:
Before we talk about product attributes in Magento 2, let’s first develop a comprehensive understanding of product attributes.
What is a Product Attribute?
A product attribute defines a characteristic of a particular product. Product attributes are visible to customers and affect their purchase decisions. Examples of product attributes include price, volume, size, colour, quality, etc. Adding a new & important product attribute is one of the most popular operations in Magento.
Product attributes are used frequently in various product related operations and thus are a powerful way to solve practical tasks. In this article, we are going to see how to add a new product attribute programmatically. Why programmatically when Magento already has a user-friendly way to add attributes?
Why You Need to Add Magento 2 Product Attribute Programmatically
The Magento 2 add product attribute programmatically can come in handy due to a wide range of reasons. For example:
Bulk Actions: When using the admin panel to add attributes in bulk, you may notice the system starts to lag. Even if there is no lag, the process is extremely time-consuming and resource intensive. By automating the entire process of create product attribute in Magento 2 programmatically, merchants can save valuable time and effort.
Third Party Integration: If your product data comes from third party sources such as a CRM, then the Magento 2 create product attribute programmatically can prove highly effective. It will automatically fetch the data, thereby streamlining the entire process. This ensures there is no mismatch between the store and third-party data.
Consistency: The Magento 2 create attribute programmatically feature ensures consistency in product attributes across the development, staging, and production environments.
Steps to Create Product Attribute in Magento 2 Programmatically
Follow these steps to create product attributes programmatically:
Step 1: Create the Setup File InstallData.php
Start by creating the setup file:
File Path: app/code/Company/Mymodule/Setup/InstallData.php
namespace Company\Mymodule\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;
}
}
Step 2: Define the install() Method
After creating the setup file, define the installation method. a programmable function is written below for this.
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
}
Step 3: Create Product Attribute Programmatically
As per your need, create new product attribute of any type like text field, drop-down, radio button etc. With below code, you can create a custom text field.
namespace Company\Mymodule\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;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'sample_attribute',
[
'type' => 'text',
'backend' => '',
'frontend' => '',
'label' => 'New Product Atrribute',
'input' => 'text',
'class' => '',
'source' => '',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
}
}
Step 4: Upgrade
Open terminal/SSH and navigate to Magento 2 setup root directory and run the commands below.
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
This will add the new product attribute titled ‘New Product Attribute’.
Final Thoughts on Magento 2 Add Product Attribute Programmatically
This concludes our article on how to add Magento 2 product attribute programmatically. The instructions are easy to follow but if you do encounter any issue in creating product attribute programmatically in Magento 2, then feel free to contact our support team for a quick fix.
Read More Magento 2 Blogs:
This blog was created with FME's SEO-friendly blog