Contact us: +91 9010449276 | Email Us: info@hackrons.com
How to create custom attributes in Magento 2?

How to create custom attributes in Magento 2?

Product attributes

Magento 2 uses attributes to filter and manage the information assigned with products. An attribute is a value of a product, for example, the product color, the size, or the description. Some attributes are built into the system by Magento default, and others can be created to add the functionality of specific needs. You can add, edit, or remove product attributes in Magento 2 to customize your products.

Attribute sets

The attribute set represents a list of attributes that are related to a particular product family. After creating the attributes, we organize and group them into different attribute sets and assign them, particular attribute groups.

Each product has a unique configuration, thus you need different dedicated attribute sets specific to each product family. For example, shoes and cameras require different specific types of information — shoes could have different colors, brands, and sizes, while a camera could have item weight, optical zoom, megapixels, screen size, iso range, etc.,

That’s why one of the first steps, when you create a product in Magento 2, is to choose the attribute set that is used as a template for the product record.

how to create custom attributes in magento2

System attributes vs. custom attributes

Attributes that are built into the core Magento 2 platform are called “system attributes”, such as price, description, gallery, image, category_ids, price, sku, tier_price, weight, etc.,

Even though you can add, edit or remove the attributes, you can’t remove the system attributes that are part of the core of the Magento 2 catalog. When viewing from the Admin | Stores | (Attributes) Product, you can see there are 46 system attributes (in default Magento 2.3.2). You should filter the grid by System: Yes, as shown in the screenshot below:

Here I was created the custom attributes and values for our requirement.

I have done in my case. I have custom options ex. L1, L2, and L3. I need to get them on custom attributes as values. So I have created a source file in a module

Vendor_Name\Module_Name\Model\Config\Source\Options.php

this file contains the small code to create the options, Here you can follow the code.

<?php
   /**
    * Copyright © 2013-2017 Magento, Inc. All rights reserved.
    * See COPYING.txt for license details.
    */
   namespace Vendor\module\Model\Config\Source;
   /**
    * Catalog category landing page attribute source
    *
    * @author      Magento Core Team <core@magentocommerce.com>
    */
   class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
   {
       /**
        * {@inheritdoc}
        * @codeCoverageIgnore
        */
       public function getAllOptions()
       {
           if (!$this->_options) {
               $this->_options = [
                   ['value' => 'l1', 'label' => __('L1')],
                   ['value' => 'l2', 'label' => __('L2')],
                   ['value' => 'l3', 'label' => __('L3')],
               ];
           }
           return $this->_options;
       }
         /**
        * Get options in "key-value" format
        *
        * @return array
        */
       public function toArray()
       {
           return [
               'l1' => __('L1'),
               'l2' => __('L2'),
               'L3' => __('L3'),
               ];
       }	
 
   }?>

Then after in your installdata.php, you have to call this as the source.

$eavSetup->addAttribute(
           Category::ENTITY,
           'category_level_rendering',
           [
               'type' => 'varchar',
               'backend' => '',
               'frontend' => '',
               'label' => 'Category Level rendering',
               'input' => 'select',
               'required' => false,
               'sort_order' => 100,
               'source' => '',
               'visible'  => true,
               'source' => 'vendor\module\Model\Config\Source\Options',
               'default'  => '0',
               'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
               'group' => 'General Information',
               'used_in_product_listing' => true,
            ]
       );

Save it, flush cache and please check.

Hopefully, it helps you.

Please give me a reply if it works for you.

If you need more assistance or any help please contact me through the mail here and also visit our site by click here and follow us @twitter, Facebook.

Also, check our related posts on magento2

  1. how to integrate AWS CloudFront CDN with Magento 2.3?
  2. How to install a Magento 2 module or extension?

Leave a Reply

Close Menu