Magento 2 How to add custom attribute to customer address and how to insert value in that attribute?
-
14-04-2021 - |
Question
I want to add a new custom attribute to "customer_address" that I did with an attribute installer script, now I am able to see that field in "eav_attribute" table. I have created a module and I have one observer for "custmer_save_after" in that. Now I want to set a custom value for that custom attribute which I created from the script.
how can I achieve this functionality?
Solution
I have created it with below code in InstallData.php file.
namespace Module\Name\Setup; use Magento\Customer\Setup\CustomerSetupFactory; use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet; use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory; use Magento\Framework\Setup\InstallDataInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\ModuleDataSetupInterface; /** * @codeCoverageIgnore */ class InstallData implements InstallDataInterface { /** * @var CustomerSetupFactory */ protected $customerSetupFactory; /** * @var AttributeSetFactory */ private $attributeSetFactory; /** * @param CustomerSetupFactory $customerSetupFactory * @param AttributeSetFactory $attributeSetFactory */ public function __construct( CustomerSetupFactory $customerSetupFactory, AttributeSetFactory $attributeSetFactory ) { $this->customerSetupFactory = $customerSetupFactory; $this->attributeSetFactory = $attributeSetFactory; } /** * {@inheritdoc} */ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]); $customerAddressEntity = $customerSetup->getEavConfig()->getEntityType('customer_address'); $attributeSetId = $customerAddressEntity->getDefaultAttributeSetId(); /** @var $attributeSet AttributeSet */ $attributeSet = $this->attributeSetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute('customer_address', 'location_id', [ 'type' => 'int', 'label' => 'Location Id', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'sort_order' => 13, 'position' => 13, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'location_id') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId ]); $attribute->save(); } }
then run the below commands:
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f (optional in `develop` mode)
php bin/magento cache:clean
php bin/magento cache:flush
OTHER TIPS
------- CREATE CUSTOMER ADDRESS ATTRIBUTE --------
Step 1 : create app/code/Dckap/CustomerAddressAttribute
Step 2 : Create app/code/Dckap/CustomerAddressAttribute/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Dckap_CustomerAddressAttribute" setup_version="1.0.0">
</module>
</config>
Step 3 : Create app/code/Dckap/CustomerAddressAttribute/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Dckap_CustomerAddressAttribute',
__DIR__
);
Step 4 : Create app/code/Dckap/CustomerAddressAttribute/Setup/InstallData.php
<?php
namespace Dckap\CustomerAddressAttribute\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* Init
*
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
if (version_compare($context->getVersion(), '1.0.0') < 0){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSetup = $objectManager->create('Dckap\CustomerAddressAttribute\Setup\CustomerSetup');
$customerSetup->installAttributes($customerSetup);
}
}
}
Step 5 : Create app/code/Dckap/CustomerAddressAttribute/Setup/CustomerSetup.php
<?php
namespace Dckap\CustomerAddressAttribute\Setup;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;
class CustomerSetup extends EavSetup {
protected $eavConfig;
public function __construct(
ModuleDataSetupInterface $setup,
Context $context,
CacheInterface $cache,
CollectionFactory $attrGroupCollectionFactory,
Config $eavConfig
) {
$this -> eavConfig = $eavConfig;
parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
}
public function installAttributes($customerSetup) {
$this -> installCustomerAttributes($customerSetup);
$this -> installCustomerAddressAttributes($customerSetup);
}
public function installCustomerAttributes($customerSetup) {
}
public function installCustomerAddressAttributes($customerSetup) {
$customerSetup -> addAttribute('customer_address',
'custom_address_attribute',
[
'label' => 'Customer Address Attribute',
'system' => 0,
'user_defined' => true,
'position' => 100,
'sort_order' =>100,
'visible' => true,
'default_value' => '',
'note' => '',
'type' => 'varchar',
'input' => 'text',
]
);
$customerSetup -> getEavConfig() -> getAttribute('customer_address', 'custom_address_attribute')->setData('is_user_defined',1)->setData('default_value','')-> setData('used_in_forms', ['adminhtml_customer_address', 'customer_register_address', 'customer_address_edit']) -> save();
}
public function getEavConfig() {
return $this -> eavConfig;
}
}
Finaly run command
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:flush
Please refer to my this module https://github.com/rashigoyal14/main/blob/master/Createaccount.zip
All coded there
Thanks Rashi