How to create a product attribute using Data Patches in Magento 2.3
Magento 2

How to create a product attribute using Data Patches in Magento 2.3

This is a short tutorial in which we will learn to create a product attribute using Data Patches in Magento 2.3

author

Paul Mestereaga

March 26, 2020

This is a short tutorial in which we will learn to create a product attribute using Data Patches in Magento 2.3

Check also my article here about Declarative Schema to learn how to manipulate the database in a complete new way starting from Magento 2.3

Context

data patch is a class that contains data modification instructions. It is defined in a <Vendor>/<Module_Name>/Setup/Patch/Data/<Patch_Name>.php file and implements \Magento\Framework\Setup\Patch\DataPatchInterface.

schema patch contains custom schema modification instructions. These modifications can be complex. It is defined in a <Vendor>/<Module_Name>/Setup/Patch/Schema/<Patch_Name>.php file and implements \Magento\Framework\Setup\Patch\SchemaPatchInterface.

Unlike the declarative schema approach, patches will only be applied once. A list of applied patches is stored in the patch_list database table. An unapplied patch will be applied when running the setup:upgrade from the Magento CLI.

Data patches have three important methods: getDependenciesgetAliasesapply

Let’s say we want to create an attribute for the products in which we want to store an embedded video code. So we want to create a new attribute video.

Create the Data Patch class

Lets create the file <Vendor>/<Module_Name>/Setup/Patch/Data/AddVideoToProduct.php in which we define the class AddVideoToProduct.

This class needs to implement the DataPatchInterface interface and implement the methods getDependenciesgetAliasesapply

To create the product attribute we need to use Magento\Eav\Setup\EavSetupFactory so we would use DI to inject it into out class.

See the full code bellow:

<?php
namespace Vendor\Module\Setup\Patch\Data;
 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\Patch\DataPatchInterface;
 
class AddVideoToProduct implements DataPatchInterface
{
    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;
    /** @var EavSetupFactory */
    private $eavSetupFactory;
 
    /**
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->moduleDataSetup = $moduleDataSetup;
    }
    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();
 
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'video',
            [
                'type' => 'text',
                'frontend' => '',
                'label' => 'Video',
                'input' => 'textarea',
                'class' => '',
                'source' => '',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => true,
                'used_in_product_listing' => false,
                'is_wysiwyg_enabled'      => false,
                'unique' => false,
                'apply_to' => ''
            ]
        );
        $this->moduleDataSetup->getConnection()->endSetup();
    }
 
    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [];
    }
 
    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }
}

After we define the class we need to run:

php bin/magento setup:upgrade

Conclusion

It’s fairly easy to create a product attribute using Data Patches in Magento 2.3, not that different from Upgrade Data Scripts. Let me know what you think and if you already started using Declarative Schema and Data and Schema Patches.

You can find more info on Data Patches on the official docs here.

Is your Magento slow?

Wondered how you can make your Magento load faster? Wonder no more. Here is a FREE step by step guide that helps you.

Related Posts

Declarative schema in Magento 2.3+

Declarative schema in Magento 2.3+

16 Mar 2020

|

Paul Mestereaga

In this tutorial we will learn the how to configure a declarative schema in Magento 2.3, perform database operations using declarative schema, create a schema whitelist and create data and schema patches.

Is Magento 2 slow on localhost?

Is Magento 2 slow on localhost?

20 Feb 2020

|

Paul Mestereaga

A fresh install of Magento 2 slow on localhost? Here are some steps you can take to make your Magento 2 faster on your localhost.

optimize-magento-preview

FREE!

Magento 2
Optimization guide

Wondered how you can make your Magento load faster? Wonder no more. Here is a step by step guide that helps you.