How to Create Customer Attribute Using Data Patch

Blog
Spread the love

Today we will learn how we can create a custom customer attribute through a data patch but first we
should know a little bit about data patches.

Data Patch:

  • It is a class that stores instructions for data modification.
  • InstallData and UpgradeData were used in Magento 2.1 and 2.2. This script works in Magento 2.3.x also but being a Professional Developer one should use a data patch to add or modify EAV data.
  • Magento 2.3.x has introduced the Data Patch Interface Magento\Framework\Setup\Patch\DataPatchInterface to add or modify the EAV data.
  • DataPatchInterface must implement three functions: apply(), getDependencies() and getAliases()
  • All data patches should be inside app/code/[vendor_name]/[module_name]/Setup/Patch/Data.

Note: Patches will only be applied once and stored in the patch_list database table.

To create a custom customer attribute through a data patch, follow the steps which are given below:

Step 1: Create Basic Module

Create registration.php file under app/code/[vendor_name]/[module_name]

(In my case, vendor_name is ‘Bluethink’ and module_name is ‘CustomerAttribute’)

Now, create module.xml file under app/code/Bluethink/CustomerAttribute/etc

Step 2: Create a Data Patch file

Create CustomAttribute.php file under app/code/Bluethink/CustomerAttribute/Setup/Patch/Data

In Last, Now just execute the below command:

apply() function is used to define your core logic for installing/upgrading data to the database.

getAliases() function defines aliases for the patch class. In this case, the class name could possibly
change, and if it does, we should supply the old class-name here, so that it doesn’t get executed a
second time.

getDependencies() function contain the class name of dependent patches. This functionality notifies
Magento to execute the “patches”. We first define it here, before the patch script.

Thanks for reading my blog. I hope you would learn a lot of things from this blog. Keep liking and
sharing!!

bluethinkinc_blog

bluethinkinc_blog

2022-08-09

0

Leave a Reply

Your email address will not be published. Required fields are marked *

Find More Blogs

How to Add a Custom Tab to the Customer Order Detail Page in Magento 2

Spread the love Adding a custom tab to the Order Detail

Exception Handling in Java

Spread the love Introduction – Exception:An unwanted

How to add custom less file in Magento2

Spread the love Source files *.css included into layout

Spring Boot Microservices

Spread the love Microservices architecture has become

Implementation of WireMock for better testing

Spread the love WireMock is a flexible and powerful tool

Magento 2: How to selected default option in configurable options?

Spread the love Configurable products in Magento 2 allow

How To Implement Simple JWT Authentication in Django Rest Framework

Spread the love We’ll learn how to use simple JWT

Optimizing Search: Integrating Elasticsearch in Django Applications

Spread the love What is Elasticsearch? Elasticsearch is a

How to create Customer Segments

Spread the love In Magento 2, “customer

Proxy Design Pattern and Requisition List in Magento 2

Spread the love Requisition List in Magento 2 Requisition

bluethinkinc Blogs