Magento provides so many default fields but If we need custom information in customer address then we can create a new customer_address type attribute.
In this article, I will share how we can add text type attribute in Customer Address,
You need to create InstallData.php file in your existing module or you can create a new module as well as,
If you are creating in your existing module: app/code/[Vendor Name]/[Module Name]/Setup/InstallData.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 |
<?php namespace [Vendor Name]/[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; class InstallData implements InstallDataInterface { /** * @var CustomerSetupFactory */ private $customersetupFactory; /** * @var AttributeSetFactory */ private $attributesetFactory; /** * @param CustomerSetupFactory $customersetupFactory * @param AttributeSetFactory $attributesetFactory */ public function __construct( CustomerSetupFactory $customersetupFactory, AttributeSetFactory $attributesetFactory ) { $this->customersetupFactory = $customersetupFactory; $this->attributesetFactory = $attributesetFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $customerSetup = $this->customersetupFactory->create(['setup' => $setup]); $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer'); $attributeSetId = $customerEntity->getDefaultAttributeSetId(); $attributeSet = $this->attributesetFactory->create(); $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId); $customerSetup->addAttribute('customer_address', 'job_title', [ 'type' => 'varchar', 'label' => 'Job Title', 'input' => 'text', //You can change your input type as per your requirement 'required' => false, //If You need this field is required in customer address area then you can just set "true" in stand of "false" 'visible' => true, 'user_defined' => true, 'sort_order' => 1000, 'position' => 1000, 'system' => 0, ]); $attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'job_title') ->addData([ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, 'used_in_forms' => ['adminhtml_customer_address'], //Other list of forms: customer_address_edit, customer_register_address where you want to display the custom attribute ]); $attribute->save(); } } |
After creating the above file you have to run below commands:
1 2 |
php bin/magento setup:upgrade php bin/magento cache:clean |
Now you can check in the customer section in the admin area.
Our newly created attribute shown in the customer address form.
[crayon-63e09ab70b7e1535687474/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.