In this tutorial, we will help you to explore the way to work with Magento 2 attributes. This includes how to create and delete Magento 2 attributes as well as attribute sets. In addition, you will find out ways to import and export CSV file, and add 2 types of attribute: EAV and extension.
Table of Contents
Magento 2 attributes are predefined elements such as Name, Price, Description, and so on, which can be used for any products. Thus, they play an essential role in your store catalog which is to help your customers easily pick out what they need.
First, log in to your website’s Admin panel.
In Dashboard, navigate to Store Tab -> Select Product-> Click on the button Add New Attribute.
In the new page, all your Magento 2 attributes will be displayed. To add a new one, click “Add New Attribute†button in the upright corner.
Now, you can start customizing your attribute.
In the ATTRIBUTE INFORMATION box, click on Properties, then customize the blank fills.
After configuring the Size attribute as the picture above you will get this.
Next, customize your attribute in Advance Attribute Properties section in the page bottom.
Navigate to the ATTRIBUTE INFORMATION box again and click on Manage Labels, then you will get this page.
Default Store View: The attribute label shown to the customer in the frontend
In case your store is available in several languages, fill in a translated title of an attribute for each store view in this section.
Finally, click Save Attribute to save all the procedure.
After creating Magento 2 attributes, go to STORES -> Products, you will see a list of attributes. Now, click to the one you want to delete.
Then, you will be lead to the edit page of that attribute. There’s Delete Attribute option in the toolbar. Click on it and you’re done.
An attribute set is a list of attributes which show all the characteristics of a product. It serves as a template for a new product. Thus, assigning a product to a specific attribute set are helpful in many aspects:
To create an attribute set, you need:
Step 1: Log in to the Admin Panel.
Step 2: In the Admin Panel, navigate to Store.
Step 3: Then, in the opened box, click on Attribute Set.
Step 4: Next, the new page, click on Add Attribute Set, then start customizing.
Enter Name of your new attribute.
In the Base On dropdown menu, if you are creating your very first attribute set, there will be only Default option. However, if you’ve already have other attribute sets, this new one will be a kind of sub attribute for the one you choose.
You’ve got the gist of what you can do to create new products and Magento 2 attributes. Yet, if you have a huge number of products, the manual way of creating one by one doesn’t seem to be a good choice. With that being said, here’s the shortcut for you – to import CVS file.
In Dashboard, go to System.
In the opened box, click Import.
In case you want to know what CSV file will be look like, Download Sample File to check it out. Here is the sample file.
Now, you can input as many products as you want.
Next, upload your product images.
Especially, make sure your CSV file includes the following rows:
Base_image
Base_image_label
Thumbnail_image
Thumbnail_image_label
Small_image
Small_image_label
Then, upload all the images of the product in your Magento 2 pub/media/import folder. The images’ names in the folder and in CSV file must be the same.
In the end, your CSV file will be like this.
Now, back to the import page.
In the Import Behavior section, set the Import Behavior’s dropdown to Add / Update
In File to Import section, click Browse and select CSV file form your computer.
Next, enter /pub/media/import in Image File Directory.
Then, click Check Data button in the upright corner.
If successful, you will get this message.
Click Import.
Now, you can check your work by navigating to PRODUCTS -> Catalog.
In Dashboard, click Settings -> Export.
Now, in the Export Settings section, choose Products in the Entity Type’s dropdown menu.
Next, set Export File Format to CSV.
Then, you will see the Entity Attributes section displayed.
Exclude the unnecessary attributes from the list by selecting them and then click on Continue from the bottom of the page, then you’re done.
First, create eavsetup factory.
Next, run the following script and complete the creating EavSetup factory.
/**
* @var EavSetupFactory
*/protected $eavSetupFactory;
/**
* UpgradeData constructor
*
* @param EavSetupFactory $eavSetupFactory
*/public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
Next, add EAV attribute.
/** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'is_featured',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Is Featured',
'input' => 'boolean',
'class' => '',
'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'user_defined' => false,
'default' => '1',
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => false,
'unique' => false,
'apply_to' => ''
]
);
$entityTypeId = 4; // Find these in the eav_entity_type table
$eavSetup->removeAttribute($entityTypeId, 'is_featured');
First, create app/code/landofcoder/DemoModule/etc/extension_attributes.xml.
Next, clear the var/generation when you run setup:di:compile command.
Now, you can set these Magento 2 attributes value by creating the instance of Magento/Checkout/Api/Data/ShippingInformationInterface.php interface.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$addressInformation = $objectManager->create('Magento\Checkout\Api\Data\ShippingInformationInterface');
$extAttributes = $addressInformation->getExtensionAttributes();
$selectedShipping = $extAttributes->getCustomShippingCharge(); //get custom attribute data.
Hope that this tutorial will help you get the basis of Magento 2 attributes. You can feel free to leave any comments in the Comment Section below. If you find this tutorial useful to you, do not forget to Save and Share with others. Thank you!
The Ultimate Magento 2 Extension Tutorials
How to Create and Move Magento 2 Block
How to uninstall Magento 2 Extension
Magento 2 is an incredibly flexible and powerful eCommerce platform that can be tailored to…
Black Friday and Cyber Monday – BFCM 2024 are nearly around the corner, and it’s…
When it comes to e-Commerce platforms, undoubtedly Magento 2.0 grabs the spotlight. Magento might not…
The holiday shopping season is here, and there’s no better time to supercharge your Magento…
In today’s digital landscape, the rapid evolution of advanced web applications, particularly Progressive Web Apps…
As eCommerce booms, big data eCommerce will be all about providing insight into how to…
View Comments
Great Work! Attribute are used for multiple purposes in Magento. There’s a very easy to follow guide for creating an attribute and attribute set programmatically in Magento on magenticians. I found it very easy to understand, I hope it helps others too. thanks