cancel
Showing results for 
Search instead for 
Did you mean: 

Add extra customer address data

Re: Add extra customer address data

I tried a different approach overriding Address FormPost controller and it worked for me. Here is the steps that I followed:

1. app/code/Company/Module/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
... <preference for="Company\Module\Model\Data\Address" type="Magento\Customer\Model\Data\Address" />
<preference for="Magento\Customer\Controller\Address\FormPost" type="Company\Module\Controller\Address\FormPost" />
... </config>

2. app/code/Company/Module/Model/Data/Address.php

<?php

namespace Company\Module\Model\Data;

/**
 * Description of Address
 *
 * @author Japhet Perez
 */
class Address extends \Magento\Customer\Model\Data\Address implements \Company\Module\Api\Data\AddressInterface
{
    
    /**
     * {@inheritdoc}
     */
    public function getAddressReference()
    {
        return $this->_get(self::ADDRESS_REFERENCE);
    }

    /**
     * {@inheritdoc}
     */
    public function setAddressReference($addressReference)
    {
        return $this->setData(self::ADDRESS_REFERENCE, $addressReference);
    }
}

3. app/code/Company/Module/Api/Data/AddressInterface.php

 

<?php

namespace Company\Module\Api\Data;

use Magento\Customer\Api\Data\AddressInterface as MainAddressInterface;

/**
 * Description of AddressInterface
 *
 * @author Japhet Perez
 */
interface AddressInterface extends MainAddressInterface
{
    const ADDRESS_REFERENCE = 'address_reference';
    
    /*
     * Get Address Reference
     * @return string
     */
    public function getAddressReference();
    
    /*
     * Set Address Reference
     * @param string $addressReference
     * @return $this
     */
    public function setAddressReference($addressReference);
}

4. app/code/Company/Module/Controller/Address/FormPost.php

<?php

namespace Company\Module\Controller\Address;

/**
 * Description of FormPost
 *
 * @author Japhet Perez
 */
class FormPost extends \Magento\Customer\Controller\Address\FormPost
{
    /**
     * Process address form save
     *
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {
        $redirectUrl = null;
        if (!$this->_formKeyValidator->validate($this->getRequest())) {
            return $this->resultRedirectFactory->create()->setPath('*/*/');
        }

        if (!$this->getRequest()->isPost()) {
            $this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
            return $this->resultRedirectFactory->create()->setUrl(
                $this->_redirect->error($this->_buildUrl('*/*/edit'))
            );
        }

        try {
            $address = $this->_extractAddress();
            /* Override starts here */
            $address->setCustomAttribute('address_reference', $this->getRequest()->getPost('address_reference'));
            /* Override ends here */

            $this->_addressRepository->save($address);
            $this->messageManager->addSuccess(__('You saved the address.'));
            $url = $this->_buildUrl('*/*/index', ['_secure' => true]);
            return $this->resultRedirectFactory->create()->setUrl($this->_redirect->success($url));
        } catch (InputException $e) {
            $this->messageManager->addError($e->getMessage());
            foreach ($e->getErrors() as $error) {
                $this->messageManager->addError($error->getMessage());
            }
        } catch (\Exception $e) {
            $redirectUrl = $this->_buildUrl('*/*/index');
            $this->messageManager->addException($e, __('We can\'t save the address.'));
        }

        $url = $redirectUrl;
        if (!$redirectUrl) {
            $this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
            $url = $this->_buildUrl('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
        }

        return $this->resultRedirectFactory->create()->setUrl($this->_redirect->error($url));
    }
}

This worked for me creatingand editing addresses from customer My Account. I hope this works for you too.

 

Regards from Mexico!
Japhet

Re: Add extra customer address data

AWESOME Smiley Happy

 

 

But please change :

 

<preference for="Company\Module\Model\Data\AddressInterface" type="Magento\Customer\Model\Data\Address" />

TO

 

<preference for="Magento\Customer\Model\Data\Address" type="Company\Module\Model\Data\Address" />

 

Re: Add extra customer address data

Thanks!