Magento 2 Add Product Attribute with Options Programmatically

In this article I am going to explain how to create product attribute with options programmatically in Magento 2.

First of all you need to create one module. If you already have a module then go with that but don't forget to change the setup version and if you are new in Magento then check our another article How To Create a Magento 2 Module.

Let's get started!!

Now I assume you already have magento module.

In this article we are going to create attribute with it's options, so first of all we need to create source model file.

Create CustomAttributeOptions.php file at app/code/Devhooks/HelloWorld/Model/Config/Source/CustomAttributeOptions.php and paste the below code.

<?php namespace Devhooks\HelloWorld\Model\Config\Source; use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource; class CustomAttributeOptions extends AbstractSource { /** * Get all options * * @return array */ public function getAllOptions() { if (null === $this->_options) { $this->_options=[ ['label' => __('Option 1'), 'value' => 1], ['label' => __('Option 2'), 'value' => 2], ['label' => __('Option 3'), 'value' => 3] ]; } return $this->_options; } }

To create the product attribute with options programmatically in module we need to create InstallData class in our Magento 2 Extension and add our CustomAttributeOptions source model.

Let's Create InstallData.php file at app/code/Devhooks/HelloWorld/Setup/InstallData.php and paste the below code.

<?php namespace Devhooks\HelloWorld\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, 'custom_attribute', [ 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'Custom Atrribute', 'input' => 'select', 'class' => '', 'source' => 'Devhooks\HelloWorld\Model\Config\Source\CustomAttributeOptions', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'visible' => true, 'required' => true, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'used_in_product_listing' => true, 'unique' => false, 'apply_to' => '' ] ); } }

In above code we have create a select type attribute and add source model Devhooks\HelloWorld\Model\Config\Source\CustomAttributeOptions for get it's options. Replace, custom_attribute with your attribute code and change the lable and other attribute setting as per your need.

Now, open Command line in folder root of magento and run both commands for create new select type product attribute with options programmatically.

php bin/magento setup:upgrade php bin/magento cache:flush php bin/magento indexer:reindex

Bingo!! we are done, now go to the Admin > Store > Attributes > Product and find your attribute.

That's it!!

If you want to remove any product attribute programmatically then check our another article Magento 2 Remove Product Attribute Programmatically

I hope this article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other questions on that.

P.S. Do share this article with your team.

Review other articles maybe it'll help you too.

Recent Articles
Chrome Extension
Copyright © 2024 All rights reserved.
Ads OFF toggle_off