I added a customer custom attribute as customer_address
type and it runs correctly in the admin and in onepagecheckout as well as in shipping & billing address.
I created:
my_namespace/my_module/etc/module.xml
and registration.php
composer.json
files in the module base directory.
my_namespace/my_module/Setup/InstallData.php
namespace Namespace\Module\Setup;
use Magento\Framework\Module\Setup\Migration;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class InstallData implements InstallDataInterface
{
/**
* Customer setup factory
*
* @var CustomerSetupFactory
*/
private $customerSetupFactory;
/**
* Init
*
* @param CustomerSetupFactory $customerSetupFactory
*/
public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var CustomerSetup $customerSetup */
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
// insert attribute
$customerSetup->addAttribute('customer_address', 'attr_code', [
'label' => 'My attribute',
'type' => 'varchar',
'input' => 'text',
'position' => 45,
'visible' => true,
'required' => false,
'system' => 0
]);
$MyAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'attr_code');
$MyAttribute->setData(
'used_in_forms',
['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address']
);
$MyAttribute->save();
$setup->endSetup();
}
}
Now I need to add attribute field in the customer add
and edit
address form that are related to the file magento_customer/view/frontend/templates/address/edit.phtml
I added the field but I'm not able to get and save the value of that attribute.
Best Answer
Custom customer attributes will never just 'appear' on the frontend like they do in the backend. The code that displays them on the frontend is located in a custom phtml file.
Magento EE has this functionality built in. I'm not suggesting you NEED to spend that money, I'm just saying it has it. If you want to go ahead and attempt to add custom attributes, it's somewhat complex.
First of all, you must do all of this in a module or it just isn't going to work right, and it's going to be hard to debug/upgrade later.
You have to do these things:
You can load your custom attributes in the Block PHP. Then just add your layout to the
customer_account_create.xml
like this:This is the magic sauce to get your block PHP to load, to get your phtml to load, and to get it into the correct page.
This isn't a full answer honestly, there is a lot more to it, but you get the basic idea.