------- 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