cancel
Showing results for 
Search instead for 
Did you mean: 

Add extra customer address data

Add extra customer address data

Hello All,

I like to add extra customer address data, that also converts to quote and to order.

I only can find how to add extra attributes and database fields to existing tables. But no info an how to fill the data and retrieve the data in the frontend. And even to convert the data to the quote address, and after that to the order address.

 

I hope somebody can point me in the correct direction please.

 

Regards

Piet Janssen

22 REPLIES 22

Re: Add extra customer address data

Hello you need to create customer attribute and that attribute you need to assign used in billing, customer_account_create and edit.

 

So that attribute will be show on billing address and so customer can add that info, and magento will save that info.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Add extra customer address data

Hello, i allready done that. In the backend everything is working.

But i have allot of problems in the frontend.

The extra fields are not added to the default theme of magento.

So added it manualy.

But now the data is saved from the front but not retrieved. And did not test order process how that is going.

 

Thanks for the answer, that was no problem and there was allot of info about that.

 

Hope you can help me out.

Re: Add extra customer address data

Hello,

 

If you add new attribute, if you want to show on frontend side like for registration page and edit my account page then you need to add manually into phtml file.

 

If your attribute added into billing _form then it will show automatically.

 

Hope it will help you.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Add extra customer address data

Hello, english is not my native languages, but i will try again what problem i have.

 

Yes I added the fields manually in the phtml files.

When i enter text in those fields the data is saved to the database.

When i edit the address again the fields are empty (this is my main problem.) How to retrieve the data for that fields.

When i enter new text in those fields, the data is saved to the database and updated with the new data.

 

I hope my problem becomes clear now.

Re: Add extra customer address data

can you please write your code here how you get value into edit mode?


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Add extra customer address data

edit.phtml

<div class="field number">
            <label class="label" for="number"><span><?php /* @escapeNotVerified */ echo __('Number') ?></span></label>
            <div class="control">
                <input type="text" name="number" value="<?php echo $block->escapeHtml($block->getAddress()->getNumber()) ?>" title="<?php /* @escapeNotVerified */ echo __('Number') ?>" class="input-text <?php /* @escapeNotVerified */ echo $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('number') ?>" id="number">
            </div>
        </div>
        <div class="field number_addon">
            <label class="label" for="number_addon"><span><?php /* @escapeNotVerified */ echo __('Number addon') ?></span></label>
            <div class="control">
                <input type="text" name="number_addon" value="<?php echo $block->escapeHtml($block->getAddress()->getNumberAddon()) ?>" title="<?php /* @escapeNotVerified */ echo __('Number addon') ?>" class="input-text <?php /* @escapeNotVerified */ echo $this->helper('Magento\Customer\Helper\Address')->getAttributeValidationClass('number_addon') ?>" id="number_addon">
            </div>
        </div>

rewrite

class Address extends \Magento\Customer\Model\Data\Address
{

    /**
     * Get the house number
     * @return int
     */
    public function getNumber()
    {
        return $this->_get('number');        
    }

    /**
     * Get the house number addon
     * @return string
     */
    public function getNumberAddon()
    {
        return $this->_get('number_addon');
    }

}

 

Re: Add extra customer address data

@TestPiet

is that customer attribute or customer address attribute?


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Add extra customer address data

The database changes

        //Add house number and addon
        $customerAddressEntity = 'customer_address_entity';
        $quoteAddress = 'quote_address';
        $salesOrderAddress = 'sales_order_address';

        $installer->getConnection()
                ->addColumn(
                        $installer->getTable($customerAddressEntity), 'number', [
                            'type' => Table::TYPE_INTEGER,
                            'unsigned' => true,
                            'comment' => 'House number'
                        ]
                );
        
        $installer->getConnection()
                ->addColumn(
                        $installer->getTable($customerAddressEntity), 'number_addon', [
                            'type' => Table::TYPE_TEXT,
                            'length' => 20,
                            'comment' => 'House number addon'
                        ]
                );
        
        $installer->getConnection()
                ->addColumn(
                        $installer->getTable($quoteAddress), 'number', [
                            'type' => Table::TYPE_INTEGER,
                            'unsigned' => true,
                            'comment' => 'House number'
                        ]
                );
        
        $installer->getConnection()
                ->addColumn(
                        $installer->getTable($quoteAddress), 'number_addon', [
                            'type' => Table::TYPE_TEXT,
                            'length' => 20,
                            'comment' => 'House number addon'
                        ]
                );
        
        $installer->getConnection()
                ->addColumn(
                        $installer->getTable($salesOrderAddress), 'number', [
                            'type' => Table::TYPE_INTEGER,
                            'unsigned' => true,
                            'comment' => 'House number'
                        ]
                );
        
        $installer->getConnection()
                ->addColumn(
                        $installer->getTable($salesOrderAddress), 'number_addon', [
                            'type' => Table::TYPE_TEXT,
                            'length' => 20,
                            'comment' => 'House number addon'
                        ]
                );

And the attributes

class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    
    /**
     * Customer setup factory
     * @var CustomerSetupFactory
     */
    private $customerSetupFactory;

    /**
     * Init
     * @param CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(\Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory)
    {
        $this->customerSetupFactory = $customerSetupFactory;
    }
    
    /**
     * Upgrade
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        $this->addCustomerAddressAttributes($installer);

        $installer->endSetup();
    }
    
    /**
     * Add customer address attributes
     * @param object $installer
     */
    protected function addCustomerAddressAttributes($installer)
    {
        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $installer]);
        
        $customerAddressEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
        $attributeSetId = $customerAddressEntity->getDefaultAttributeSetId();
        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
        
        $attributesInfo = [
            'number' => [
                'label' => 'House number',
                'type' => 'static',
                'input' => 'text',
                'position' => 71,
                'sort_order' => 71,
                'visible' => true,
                'required' => true,
                'user_defined' => true,
                'system' => 0,
            ],
            'number_addon' => [
                'label' => 'House number addon',
                'visible' => true,
                'required' => false,
                'type' => 'static',
                'input' => 'text',
                'position' => 72,
                'sort_order' => 72,
                'validate_rules' => 'a:1:{s:15:"max_text_length";i:20;}',
                'user_defined' => true,
                'system' => 0,
            ]
        ];

        foreach ($attributesInfo as $attributeCode => $attributeParams) {
            $customerSetup->addAttribute('customer_address', $attributeCode, $attributeParams);
        }

        $numberAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'number');
        $numberAttribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
        ]);
        $numberAttribute->save();

        $numberAddonAttribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'number_addon');
        $numberAddonAttribute->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address'],
        ]);
        $numberAddonAttribute->save();
    }

}

Re: Add extra customer address data

@TestPiet when you print address object, are you getting that two fields into object?


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer