You want to add custom referral code field in use customer account page. Then, you can create field for custom customer attribute.
Steps for Create a Customer Attribute for Data Patches Magento 2.
(1) <Vendorname>/<Modulename>/Setup/Patch/Data/AddCustomReferralCodeAttribute.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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Vendorname/Modulename/Setup/Patch/Data; use Magento\Catalog\Ui\DataProvider\Product\ProductCollectionFactory; use Magento\Customer\Model\Customer; use Magento\Eav\Model\Config; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Psr\Log\LoggerInterface; /** * Class AddCustomReferralCodeAttribute for Create Customer Attribute using Data Patch. * @package vendorname\modulename\Setup\Patch\Data */ class AddCustomReferralCodeAttribute implements DataPatchInterface, PatchRevertableInterface { /** * @var ModuleDataSetupInterface */ private $moduleDataSetup; /** * @var EavSetupFactory */ private $eavSetupFactory; /** * @var ProductCollectionFactory */ private $productCollectionFactory; /** * @var LoggerInterface */ private $logger; /** * @var Config */ private $eavConfig; /** * @var \Magento\Customer\Model\ResourceModel\Attribute */ private $attributeResource; /** * CustomerAttribute Constructor * @param EavSetupFactory $eavSetupFactory * @param Config $eavConfig * @param LoggerInterface $logger * @param \Magento\Customer\Model\ResourceModel\Attribute $attributeResource * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup */ public function __construct( EavSetupFactory $eavSetupFactory, Config $eavConfig, LoggerInterface $logger, \Magento\Customer\Model\ResourceModel\Attribute $attributeResource, \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup ) { $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; $this->logger = $logger; $this->attributeResource = $attributeResource; $this->moduleDataSetup = $moduleDataSetup; } /** * {@inheritdoc} */ public function apply() { $this->moduleDataSetup->getConnection()->startSetup(); $this->addCustomReferralCodeAttribute(); $this->moduleDataSetup->getConnection()->endSetup(); } /** * @throws \Magento\Framework\Exception\AlreadyExistsException * @throws \Magento\Framework\Exception\LocalizedException * @throws \Zend_Validate_Exception */ public function addCustomReferralCodeAttribute() { $eavSetup = $this->eavSetupFactory->create(); $eavSetup->addAttribute( \Magento\Customer\Model\Customer::ENTITY, ‘custom_referral_code’, [ ‘label’ => ‘Custom Referral Code’, ‘required’ => false, ‘user_defined’ => 1, ‘visible’ => true, ‘system’ => 0, ‘position’ => 117, ‘default’ => ”, ‘input’ => ‘text’, ‘is_used_in_grid’ => 1, ‘is_visible_in_grid’ => 1, ‘is_filterable_in_grid’ => 1, ‘is_searchable_in_grid’ => 1, ] ); $attributeSetId = $eavSetup->getDefaultAttributeSetId(Customer::ENTITY); $attributeGroupId = $eavSetup->getDefaultAttributeGroupId(Customer::ENTITY); $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, ‘custom_referral_code’); $attribute->setData(‘attribute_set_id’, $attributeSetId); $attribute->setData(‘attribute_group_id’, $attributeGroupId); $attribute->setData(‘used_in_forms’, [ ‘adminhtml_customer’ ]); $this->attributeResource->save($attribute); } /** * {@inheritdoc} */ public static function getDependencies() { return []; } /** * */ public function revert() { } /** * {@inheritdoc} */ public function getAliases() { return []; } } |
(2) After use this commnad :
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f -j
php bin/magento indexer:reindex
php bin/magento cache:flush
[Note] : You can check admin panel in customer account information section and custom customer attribute in eav_attribute table that attribute creates successfully or not.