We cover the concept of creating custom product attributes using Data Patch with full code. In the following example, we are creating two types of product attributes:Text and Yes/No.
First You Need to Create Custom module then follow this steps. I have shared a step by step guide to product attribute using data patch.
Step 1: Create register.php
app/code/Bluethinkinc/Productattribute/registration.php
1 2 3 4 5 6 7 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethinkinc_Productattribute', __DIR__ ); ?> |
Step 2: Create module.xml
app/code/Bluethinkinc/Productattribute/etc/module.xml
1 2 3 4 5 6 |
<?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="Bluethinkinc_Productattribute" setup_version="1.0.0"> </module> </config> |
Step 3: Create ProductAttribute.php
app/code/Bluethinkinc/Productattribute/Setup/Patch/Data/ProductAttribute.php
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
<?php declare(strict_types=1); namespace Bluethinkinc\Productattribute\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\Catalog\Model\Product; use Magento\Catalog\Model\Product\Type; use Psr\Log\LoggerInterface; class ProductAttribute implements DataPatchInterface { /** * @var ModuleDataSetupInterface */ private ModuleDataSetupInterface $moduleDataSetup; /** * @var EavSetupFactory */ private EavSetupFactory $eavSetupFactory; /** * @var LoggerInterface */ private LoggerInterface $logger; /** * @param ModuleDataSetupInterface $moduleDataSetup * @param EavSetupFactory $eavSetupFactory * @param LoggerInterface $logger */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory, LoggerInterface $logger ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; $this->logger = $logger; } /** * @return void */ public function apply() { try { $productTypes = implode(',', [Type::TYPE_SIMPLE, Type::TYPE_VIRTUAL]); /** @var EavSetup $eavSetup */ $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->addAttribute(Product::ENTITY, 'sample_yes_no', [ 'type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'Yes/No field', 'input' => 'boolean', 'class' => '', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'global' => 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' => $productTypes ]); $eavSetup->addAttribute(Product::ENTITY, 'sample_text_attribute', [ 'type' => 'text', 'backend' => '', 'frontend' => '', 'label' => 'Text field Attribute', 'input' => 'text', 'class' => '', 'source' => '', 'global' => ScopedAttributeInterface::SCOPE_GLOBAL, '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, 'apply_to' => $productTypes ]); } catch (\Exception $e) { $this->logger->critical($e); } } /** * @return array */ public static function getDependencies(): array { return []; } /** * @return array */ public function getAliases(): array { return []; } } ?> |
Step 4: After create the module if you run the command as
php bin/magento module:status
You should see the module is disable now:
List of disabled modules: Bluethinkinc_Productattribute
enable the module right now, let run the command as:
php bin/magento module:enable Bluethinkinc_Productattribute
After Enable Module Then Run this Command
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento c:f
sudo chmod -R 777 var/ pub/static generated/
Now, see the result
bluethinkinc_blog
2023-09-25