Steps of how to create product attribute using data patch in Magento2.4.6

To create a product attribute using a data patch in Magento 2.4.6, you can follow these steps:

1. Create a new module:

  • In this blog, the module name is CreateProductAttribute which is located at the following path: app/code/Esparksinc/CreateProductAttribute

2. Create the data patch file:

  • Create a data patch file in the module directory with a suitable name. For example, CreateProductAttr.php.    app/code/Esparksinc/CreateProductAttribute/Setup/Patch/Data/CreateProductAttr.php 
  • Add the following code to the data patch file:
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
/**
 * Created By : Esparks
 */
declare (strict_types = 1);
namespace Esparksinc\CreateProductAttribute\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;

/**
 * Class CreateProductAttr for Create Custom Product Attribute using Data Patch.
 */
class CreateProductAttr implements DataPatchInterface {
    /**
     * ModuleDataSetupInterface
     *
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /**
     * EavSetupFactory
     *
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * {@inheritdoc}
     */
    public function apply() {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute('catalog_product', 'product_temp_attribute', [
            'type' => 'text',
            'backend' => '',
            'frontend' => '',
            'label' => 'Product Temp Atrribute',
            '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' => '',
        ]);
    }
    /**
     * {@inheritdoc}
     */
    public static function getDependencies() {
        return [];
    }
    /**
     * {@inheritdoc}
     */
    public function getAliases() {
        return [];
    }
}

Following is the description of functions used in the CreateProductAttr Class.

  • apply(): This function is used to implement the code logic to install or upgrade data in the database.
  • GetDependencies() : This is a static method that returns an empty array. It is likely used to specify any dependencies that this class has on other classes or libraries. In this case, the class has no dependencies, so an empty array is returned.
  • getVersion(): This function will return a version of the patch. If the version of the module will be high than the version we specify in our patch, then it will not get executed. It will executed only when it’s equal to or lower than the version here.

3. Execute Magento Commands

Now you need to run these commands to enable the module and also Custom Product Attribute.

  • php bin/magento s:up
  • php bin/magento s:s:d -f
  • php bin/magento s:s:d -f
  • php bin/magento c:c
Now we can check it in the admin: Catalog => Product => add product

Caution:

Note: if you want to change the patch file you need to follow these steps otherwise the changes will not occur in the patch file because when you run this command the patch file saves in databases. php bin/magento s:up

Steps:

  • Open the “patch_list” table in your database.
  • Delete the entry of your existing patch looks like 
  • Esparksinc\CreateProductAttribute\Setup\Patch\Data\CreateProductAttr

Again execute the commands below:

php bin/magento s:up
php bin/magento s:s:d  -f
php bin/magento  c:c