Add custom product attributes for product types in Magento2.

Donations Make us online

In this blog, we are going to learn how we can add custom product attributes for specific product types.

Example:- Custom Attribute 1 for Simple Product, Custom Attribute 2 for Configurable Product, Custom Attribute 3 for Bundle Product, and so on for other Products.

Here, in the following example, I have added some custom attributes for different product types.

Please follow the below steps to achieve the desired result.

Step 1: Create CustomProductAttributes.php file inside the app/code/Webkul/CustomProductAttributes/Setup/ Patch/Data/ directory.

Searching for an experienced
Magento 2 Company ?
Read More


<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_CustomProductAttributes
 * @author    Webkul Software Private Limited
 * @copyright Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace WebkulCustomProductAttributesSetupPatchData;

use MagentoFrameworkSetupPatchDataPatchInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoEavModelConfig as EavConfig;
use MagentoEavSetupEavSetupFactory;
use MagentoEavModelEntityAttributeSetFactory;
use MagentoCatalogModelProduct;
use MagentoEavModelEntityAttributeScopedAttributeInterface;

class CustomProductAttributes implements DataPatchInterface
{
    /**
     * @var MagentoFrameworkSetupModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var MagentoEavModelConfig
     */
    private $eavConfig;

    /**
     * @var MagentoEavSetupEavSetupFactory;
     */
    private $eavSetupFactory;
    
    /**
     * @var MagentoEavModelEntityAttributeSetFactory
     */
    private $attributeSetFactory;

    /**
     * Constructor
     *
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavConfig $eavConfig
     * @param EavSetupFactory $eavSetupFactory
     * @param SetFactory $attributeSetFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavConfig $eavConfig,
        EavSetupFactory $eavSetupFactory,
        SetFactory $attributeSetFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavConfig = $eavConfig;
        $this->eavSetupFactory = $eavSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

    /**
     * Do Upgrade
     *
     * @return void
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();

        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        /** creating attribute for simple products  */
        $eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'custom_attr_simple',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Custom Attribute 1',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'simple'
            ]
        );

        /** creating attribute for configurable products */
        $eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'custom_attr_configurable',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Custom Attribute 2',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'configurable'
            ]
        );

        /** creating attribute for bundle products */
        $eavSetup->addAttribute(
            MagentoCatalogModelProduct::ENTITY,
            'custom_attr_bundle',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Custom Attribute 3',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'bundle'
            ]
        );
    }

    /**
     * @inheritdoc
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * @inheritdoc
     */
    public static function getDependencies()
    {
        return [];
    }
}

Step 2: Now, just run the below upgrade command.

php bin/magento setup:upgrade

Now, look into the first image in which Custom Attribute 1 is being displayed for Simple Products Only.

Look into the second image in which Custom Attribute 2 is being displayed for Configurable Products only.

add custom product attributes

And in the last image in which Custom Attribute 3 is being displayed for Bundle Products only.

add custom product attributes

Hope this will be helpful.

Thanks 🙂

You may like our other tutorials :

Custom router in Magento2

Custom Checkout step in Magento 2


Source link