In Today’s tutorial, we will learn about How to Add Custom Yes/No Boolean Product attributes programmatically using Data Patch in Magento 2.
Here, we will learn to add product attributes programmatically with the help of a data patch in Magento 2. A data patch is a class that stores instructions for data modification. Using a data patch we can add custom product attributes in Magento 2 by creating a PHP file.
Before starting, I’m sure you have already created your custom module,
If not follow this quick tutorial on How to create your first custom module in Magento 2.
Now create AddCallForQuoteAttribute.php in your module directory followed by Setup\Patch\Data
<?php
declare(strict_types=1);
namespace Jigar\ProductCustomAttributes\Setup\Patch\Data;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;
class AddCallForQuoteAttribute implements DataPatchInterface, PatchRevertableInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Constructor
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
*/
public function apply()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'is_call_for_quote',
[
'type' => 'int',
'label' => 'Call for Quote ?',
'input' => 'boolean',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'required' => false,
'sort_order' => '100',
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'default' => null,
'visible' => true,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'unique' => false,
'group' => 'General',
'used_in_product_listing' => false,
'is_used_in_grid' => true,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false
]
);
$this->moduleDataSetup->getConnection()->endSetup();
}
public function revert()
{
$this->moduleDataSetup->getConnection()->startSetup();
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'mandatory_posting');
$this->moduleDataSetup->getConnection()->endSetup();
}
/**
* {@inheritdoc}
*/
public function getAliases()
{
return [];
}
/**
* {@inheritdoc}
*/
public static function getVersion()
{
return '1.0.0';
}
/**
* {@inheritdoc}
*/
public static function getDependencies()
{
return [];
}
}
Using the above way you can easily add new custom yes/no (boolean) attributes in products.
You can change this according your requirement.
Thanks for visiting, See you in the next blog!!
Posts you might interested in:
Securely render Javascript inside Magento 2 template files using PHP
Leave a Comment