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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<?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