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’)
1 2 3 4 5 6 7 |
<?php declare(strict_types=1); use \Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register(ComponentRegistrar::Module, 'Bluethink_CustomerAttribute', __DIR__); |
Now, create module.xml file under app/code/Bluethink/CustomerAttribute/etc
1 2 3 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethink_CustomerAttribute" setup_version="1.0.0"/> </config> |
Step 2: Create a Data Patch file
Create CustomAttribute.php file under app/code/Bluethink/CustomerAttribute/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 110 111 112 113 114 115 116 |
<?php namespace Bluethink\CustomerAttribute\Setup\Patch\Data; use Exception; use Psr\Log\LoggerInterface; use Magento\Customer\Api\CustomerMetadataInterface; use Magento\Customer\Model\ResourceModel\Attribute as AttributeResource; use Magento\Customer\Setup\CustomerSetup; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; class CustomAttribute implements DataPatchInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var CustomerSetup */ private $customerSetup; /** * @var AttributeResource */ private $attributeResource; /** * @var LoggerInterface */ private $logger; /** * Constructor * * @param ModuleDataSetupInterface $moduleDataSetup * @param CustomerSetupFactory $customerSetupFactory * @param AttributeResource $attributeResource * @param LoggerInterface $logger */ public function __construct( ModuleDataSetupInterface $moduleDataSetup, CustomerSetupFactory $customerSetupFactory, AttributeResource $attributeResource, LoggerInterface $logger ) { $this->moduleDataSetup = $moduleDataSetup; $this->customerSetup = $customerSetupFactory->create(['setup' => $moduleDataSetup]); $this->attributeResource = $attributeResource; $this->logger = $logger; } public static function getDependencies(): array { return []; } public function getAliases(): array { return []; } public function apply() { // Start setup $this->moduleDataSetup->getConnection()->startSetup(); try { // Add customer attribute with settings $this->customerSetup->addAttribute( CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'custom_attribute', [ 'label' => 'Custom Attribute', 'required' => 1, 'position' => 100, 'system' => 0, 'user_defined' => 1, 'is_used_in_grid' => 1, 'is_visible_in_grid' => 1, 'is_filterable_in_grid' => 1, 'is_searchable_in_grid' => 1, ] ); // Add an attribute to default attribute set and group $this->customerSetup->addAttributeToSet( CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, null, 'custom_attribute' ); // Get the newly created attribute's model $attribute = $this->customerSetup->getEavConfig() ->getAttribute(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER, 'discount'); // Make attribute visible in Admin customer form $attribute->setData('used_in_forms', [ 'adminhtml_customer', 'adminhtml_customer_address', 'customer_account_edit', 'customer_address_edit', 'customer_register_address', 'customer_account_create' ]); // Save attribute using its resource model $this->attributeResource->save($attribute); } catch (Exception $e) { $this->logger->err($e->getMessage()); } // End setup $this->moduleDataSetup->getConnection()->endSetup(); } } |
In Last, Now just execute the below command:
1 2 |
php bin/magento s:up php bin/magento c:c |
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
2022-08-09