Reading list Switch to dark mode

    Create drop down product attribute in Magento 2

    Updated 9 January 2023

    Today  I am going to explain, how we can create drop down attribute for Products in Magento 2.
    There are two ways to create the drop down attribute in Magento.
    1 -> Manually : Go To admin panel,  Store -> Attributes -> Product .
    2 -> Programmatically .

    In this blog I am covering how we can create the drop down attribute programmatically,
    So let’s start :
    Step 1 : create InstallData.php file : Company\Module\Setup\InstallData.php
    Now In this file, create an Array of the attributes that you want to add as below.

    $attributeGroup = 'Test Group';
            $attributes = [
                'attribute_code' => [
                    'group'              => $attributeGroup,
                    'input'              => 'select',
                    'type'               => 'int',
                    'label'              => 'Attribute Label',
                    'visible'            => true,
                    'required'           => false,
                    'user_defined'               => true,
                    'searchable'                 => false,
                    'filterable'                 => false,
                    'comparable'                 => false,
                    'visible_on_front'           => false,
                    'visible_in_advanced_search' => false,
                    'is_html_allowed_on_front'   => false,
                    'used_for_promo_rules'       => true,
                    'source'                     => \Company\Module\Model\Config\Source\Options::class,
                    'frontend_class'             => '',
                    'global'                     =>  \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'unique'                     => false,
                    'apply_to'                   => 'simple,grouped,configurable,downloadable,virtual,bundle'
                ],...
              ];

    Step 2 : Now Add and Assign Attribute to Group and Attribute Set.

    // Add Attribute
    // Magento\Eav\Setup\EavSetupFactory  $this->eavSetupFactory
    // Magento\Framework\Setup\ModuleDataSetupInterface $setup
    
     $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
                foreach ($attributes as $attribute_code => $attributeOptions) {
                    $eavSetup->addAttribute(
                        \Magento\Catalog\Model\Product::ENTITY,
                        $attribute_code,
                        $attributeOptions
                    );
                }
    //Assign 
                foreach ($attributes as $attribute_code => $attributeOptions) {
    
                    // Class \Magento\Eav\Model\AttributeManagement
    
                    $this->attributeManagement->assign(
                        'catalog_product',
                        $attributeSet->getId(),
                        $groupId,
                        $attribute_code,
                        $attributeSet->getCollection()->getSize() * 10
                    );
                }

    Note :
    $attributeSet : you can load the attribute collection and loop for it or Can pass specific id instead.
    $groupId : Magento\Eav\Model\Entity\Attribute\Group : you can get the group id with the help of this class.

    Now , Options for drop down attribute , you can add options directly as a array.
    and another approach is to use class Magento\Eav\Model\Entity\Attribute\Source\AbstractSource  as follows :

    Searching for an experienced
    Magento 2 Company ?
    Find out More
    <?php
    
    namespace Company\Module\Model\Config\Source;
    
    class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
    {
        /**
        * Get all options
        *
        * @return array
        */
        public function getAllOptions()
        {
            $this->_options = [
                    ['label' => __('No'), 'value'=>'0'],
                    ['label' => __('Yes'), 'value'=>'1'],
                    ['label' => __('Other'), 'value'=>'2']
                ];
    
        return $this->_options;
    
        }
    
    }

    Please read each line carefully, so that you can create the attributes easily.
    and In case you have any Query then feel free to ask in comment section below.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Senthil
    • Prabhat Rawat (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home