>>
In Magento 2 sometimes we need to add additional information in customer address In this case, we can create a custom address field to store the additional information. So, in this blog, we will learn how to create a custom customer address attribute then set the value of this attribute and get the value of this attribute.
To Create or add a customer address attribute we need to create a PHP file for defining our customer address attribute name and attribute_code.To Create custom customer address attribute follow below steps:
Firstly, we will create the InstallData.php file at below path :
Mageants -> Blog -> Setup -> InstallData.php
<?php |
In Magento 2 sometimes we need to add additional information in customer address In this case, we can create a custom address field to store the additional information. So, in this blog, we will learn how to create a custom customer address attribute then set the value of this attribute and get the value of this attribute.
To Create or add a customer address attribute we need to create a PHP file for defining our customer address attribute name and attribute_code.To Create custom customer address attribute follow below steps:
Firstly, we will create the InstallData.php file at below path :
Mageants -> Blog -> Setup -> InstallData.php
<?php use Magento\Eav\Model\Config; class InstallData implements InstallDataInterface public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) //creating a custom text field programmatically $eavSetup->addAttribute('customer_address', 'custom_address_attribute', [ $this->getEavConfig()->getAttribute('customer_address', 'custom_address_attribute')->setData('is_user_defined', 1)->setData('is_required', 0)->setData('default_value', '')->setData('used_in_forms', ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'])->save(); public function getEavConfig() { |
After Creating InstallData.php You have to run php magento setup:upgrade and php bin/magento setup:static-content:deploy then check the result at Customer -> All Customer -> Click on Edit Customer Link->Addresses -> Click on Select and Edit Customer Link.
In the above code our attribute code is “custom_address_attribute” and label is “Custom Address Attribute”.
To set Custom Customer Address Attribute value we take Magento\Customer\Model\Address instance and created it’s object($address) and used Magento\Customer\Model\ResourceModel\AddressFactory instance and created it’s object($addressFactory).
To Set Custom Customer value, firstly we create Helper File at Mageants->Blog->Helper->Data.php
<?php class Data extends \Magento\Framework\App\Helper\AbstractHelper protected $address; protected $pageFactory; |
Now,We Create Controller file at Mageants->Blog->Controller->Index->Index.php
<?php class Index extends \Magento\Framework\App\Action\Action protected $pageFactory; |
To get Custom Customer Address Attribute value you have to use Magento\Customer\Api\AddressRepositoryInterface instance and created it’s object($addressRepositoryInterface)
To Get Custom Customer value, firstly we create Helper File at Mageants->Blog->Helper->Data.php
<?php class Data extends \Magento\Framework\App\Helper\AbstractHelper protected $address; protected $pageFactory; public function getCustomAttribute($customerId) |
Now,We Create Controller file at Mageants->Blog->Controller->Index->Index.php
<?php class Index extends \Magento\Framework\App\Action\Action protected $pageFactory; |
We hope above blog helps you to clearly understand How to Create Customer Address Attribute in Magento 2 Programmatically. In case of any kind of problem with the above code implementation, you can contact us or let us know in comment section.
Sign In
Create New Account