In this blog, we are going to learn how we can add custom product attributes for different 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/Vendor/Module/Setup/ Patch/Data/ directory.
<?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 Webkul\CustomProductAttributes\Setup\Patch\Data; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Eav\Model\Config as EavConfig; use Magento\Eav\Setup\EavSetupFactory; use Magento\Eav\Model\Entity\Attribute\SetFactory; use Magento\Catalog\Model\Product; use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; class CustomProductAttributes implements DataPatchInterface { /** * @var \Magento\Framework\Setup\ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var \Magento\Eav\Model\Config */ private $eavConfig; /** * @var \Magento\Eav\Setup\EavSetupFactory; */ private $eavSetupFactory; /** * @var \Magento\Eav\Model\Entity\Attribute\SetFactory */ 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( \Magento\Catalog\Model\Product::ENTITY, 'custom_attr_simple', [ 'type' => 'text', 'backend' => '', 'frontend' => '', 'label' => 'Custom Attribute 1', 'input' => 'text', 'class' => '', 'source' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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( \Magento\Catalog\Model\Product::ENTITY, 'custom_attr_configurable', [ 'type' => 'text', 'backend' => '', 'frontend' => '', 'label' => 'Custom Attribute 2', 'input' => 'text', 'class' => '', 'source' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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( \Magento\Catalog\Model\Product::ENTITY, 'custom_attr_bundle', [ 'type' => 'text', 'backend' => '', 'frontend' => '', 'label' => 'Custom Attribute 3', 'input' => 'text', 'class' => '', 'source' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::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.

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

Hope this will be helpful.
Thanks 🙂
Be the first to comment.