Last active
July 8, 2023 22:47
-
-
Save 0-Sony/252311391be9e434a53e522709cc11be to your computer and use it in GitHub Desktop.
Magento 2 : Create Your Own Customer Attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
* @author Phuong LE <phuong.le@agence-soon.fr> <@> | |
* @copyright Copyright (c) 2016 Agence Soon (http://www.agence-soon.fr) | |
*/ | |
namespace NameSpace\Customer\Setup; | |
use Magento\Customer\Api\CustomerMetadataInterface; | |
use Magento\Customer\Model\Customer; | |
use Magento\Customer\Setup\CustomerSetup; | |
use Magento\Customer\Setup\CustomerSetupFactory; | |
use Magento\Eav\Model\Entity\Attribute\Set; | |
use Magento\Eav\Model\Entity\Attribute\SetFactory; | |
use Magento\Framework\Setup\InstallDataInterface; | |
use Magento\Framework\Setup\ModuleContextInterface; | |
use Magento\Framework\Setup\ModuleDataSetupInterface; | |
class InstallData implements InstallDataInterface | |
{ | |
/** | |
* @var CustomerSetupFactory | |
*/ | |
private $customerSetupFactory; | |
/** | |
* @var SetFactory | |
*/ | |
private $attributeSetFactory; | |
/** | |
* InstallData constructor. | |
* @param CustomerSetupFactory $customerSetupFactory | |
* @param SetFactory $attributeSetFactory | |
*/ | |
public function __construct( | |
CustomerSetupFactory $customerSetupFactory, | |
SetFactory $attributeSetFactory | |
) { | |
$this->customerSetupFactory = $customerSetupFactory; | |
$this->attributeSetFactory = $attributeSetFactory; | |
} | |
/** | |
* Installs data for a module | |
* | |
* @param ModuleDataSetupInterface $setup | |
* @param ModuleContextInterface $context | |
* @return void | |
*/ | |
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) | |
{ | |
/** @var CustomerSetup $customerSetup */ | |
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); | |
$customerSetup->addAttribute(Customer::ENTITY, 'my_custom_attribute', | |
[ | |
'type' => 'varchar', | |
'label' => 'My Custom Label Attribute', | |
'input' => 'text', | |
'required' => false, | |
'sort_order' => 30, | |
'visible' => true, | |
'system' => false, // Otherwise value won't be saved | |
'position' => 30, | |
]); | |
/** Allow us to used in grid in backoffice */ | |
$customerSetup->updateAttribute(Customer::ENTITY, 'my_custom_attribute', 'is_used_in_grid', true); | |
$attributeSetId = CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER; // The attribute set Id for customer is 1 | |
/** @var Set $attributeSet */ | |
$attributeSet = $this->attributeSetFactory->create(); | |
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); | |
/** We need to declare in which forms we can fill our data custom attribute */ | |
$usedInForms = [ | |
'adminhtml_customer', | |
'adminhtml_checkout' | |
]; | |
/** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute */ | |
$attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY,'my_custom_attribute'); | |
$attribute->addData([ | |
'attribute_set_id' => $attributeSetId, | |
'attribute_group_id' => $attributeGroupId, | |
'used_in_forms' => $usedInForms, | |
]); | |
$attribute->getResource()->save($attribute); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment