How to create an attribute programatically in Magento2

installer

While creating the extension in Magento2, it’s often necessary to create the custom attributes for the products. creating the attributes on the fly is more over same when compare to Magento1.x. But the extensions structure is different. because magento2 is more developer friendly.

We need to follow the below method to create the custom product attributes. Lets assume we are going to create the attribute called ‘warranty’ and the vendor name is ‘DCKAP’.

Step 1 : Create the extension in the Vendor directory.

app/code/ Training4/Warranty

We need to define our module in module.xml file. It should be under app/code/ Training4/Warranty

module.xml file contains the below code.


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">

<module name="Training4_Warranty" setup_version="1.0.0">

<sequence>

<module name="Magento_Catalog"/>

</sequence>

</module>

</config>

Here Training4 is a vendor name and Warranty is a extensions/module name.

 

Step 2: Create InstallData.php file under app/code/ Training4/Warranty/setup


<?php

namespace Training4\Warranty\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

{

/**

* EAV setup factory

*

* @var \Magento\Eav\Setup\EavSetupFactory EavSetupFactory

*/

private $eavSetupFactory;

/**

* Init

*

* @param EavSetupFactory $eavSetupFactory

*/

public function __construct(EavSetupFactory $eavSetupFactory)

{

$this->eavSetupFactory = $eavSetupFactory;

}

/**

* {@inheritdoc}

* @SuppressWarnings(PHPMD.ExcessiveMethodLength)

*/

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

{

/** @var EavSetup $eavSetup */

$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

/**

* Add attributes to the eav/attribute

*/

$eavSetup->addAttribute(

\Magento\Catalog\Model\Product::ENTITY,

'warranty',

[

'type' => 'varchar',

'attribute_set' => 'Gear',

'group' => '',

'backend_model' => '\Training4\Warranty\Model\Product\Attribute\Backend\Warranty',

'frontend_model' => '\Training4\Warranty\Model\Product\Attribute\Frontend\Warranty',

'label' => 'Warranty',

'input' => 'text',

'class' => '',

'source' => '',

'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_GLOBAL,

'visible' => true,

'required' => false,

'user_defined' => true,

'default' => 0,

'searchable' => false,

'filterable' => false,

'comparable' => false,

'visible_on_front' => true,

'used_in_product_listing' => true,

'unique' => false,

'apply_to' => ''

]

);

$attribute_set_id=$eavSetup->getAttributeSetId('catalog_product', 'Gear');

$attribute_group_id=$eavSetup->getAttributeGroupId('catalog_product', $attribute_set_id, 'General');

$attribute_id=$eavSetup->getAttributeId('catalog_product', 'warranty');

$eavSetup->addAttributeToSet('catalog_product',$attribute_set_id, $attribute_group_id, $attribute_id);

}

}

If you take a look at the function public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) you can understand how we are adding the attributes to the particular attribute sets. it’s a self explanatory.

Run the below command to enable the extension.

php magento module:enable Training4_Warranty

Now the extension is enabled. you can clear the cache under var directory and then run the below command to update the attribute in data base.

php magento setup:db:status

php magento setup:upgrade

That’s it. Now you can navigate to back end and see the newly created attribute.

Leave a Comment

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


+ 8 = 11