Notes from the Team

Add & Remove Product Attributes

Magento

Magento Logo

Our last tutorial introduced you to Magento’s Product Types, but we are still not quite ready to add products to the store database. First, we need to set up product attributes that our products may have. Magento comes with a pre-defined set of Defult attributes like size and color, and these may be all you need. However, if you have specialty products, you may need to create additional attributes. This can be done using Admin Panel, but this tutorial will teach you to create attributes programmatically with a custom module.

Need help with custom modules like these? The Richmond, Virginia Magento experts at Ameronix are here for you.

Adding an attribute

To programmatically add a product attribute, create a module with the files and code below. Where an array of key-value pair parameters are passed in to the addAttribute() function, you may change the values as your needs require.

Namespace/ModuleName/etc/module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Namespace_ModuleName" setup_version="0.0.2" active="true" />
</config>

Namespace/ModuleName/registration.php

<?php

namespace Namespace\ModuleName\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,
            'sample_attribute_1',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Example Attribute',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => 
                    \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_WEBSITE,
                '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
            ]
        );
    }
}

There are additional parameters that can be passed to addAttribute, and the resulting behaviors are as follows:

  • If neither the group nor attribute_set parameters are defined and user_defined is set to false (as in the example above), the attribute will be created in all attribute sets under the Product Details group. If user_defined is instead set to true, the attribute will be created in all attribute sets, but will be unassigned.

  • If the the group parameter is defined, Magento will ignore the attribute_set parameter and create the attribute in ALL attribute sets under the specified group. If the specified group does not exist, it will be created.

  • If the attribute_set parameter specifies an existing attribute set, but the group parameter is not defined, the attribute will be created in the specified attribute set but will be unassigned. If the specified attribute set does not exist, it will NOT be created, and the attribute will be assigned to all attribute sets under the Product Details group.

Removing an attribute

To programmatically delete an attribute (in this case ‘example_attribute’, use the module from above, but in InstallData.php change $eavSetup->addAttribute(...) to the function below

$eavSetup->removeAttribute(
    \Magento\Catalog\Model\Product::ENTITY,
    'example_attribute'
);

Note

You may have seen install() functions like the ones above between $setup->startSetup() and $setup->endSetup() functions. Adding this code to the Remove Attribute module will results in attribute data not being removed from all of the associated database tables, so I recommend leaving it out. In fact, these functions should be used very rarely. More explanation on what these functions do can be found here.

In our next post…

Learn how to Import Products from CSV files.

About The Author

Nikki Aaron
Nikki Aaron

Web Developer

The Next Step

Connect

[email protected] (804) 272-1200