cancel
Showing results for 
Search instead for 
Did you mean: 

Email does not save in quote_address table until after order is placed

Email does not save in quote_address table until after order is placed

Email does not save in quote_address table until after order is placed. How can I save email in quote_address table along with all other address information in quote_address table at the same time as the other address information (ie: right after customer clicks on the Next button, after they have filled in their address details)?
2 REPLIES 2

Re: Email does not save in quote_address table until after order is placed

@thaiahoygmed08 

 

Hey,

 

You can simply create a custom ajax request to save email address on Next button click by adding custom javascript file in checkout_index_index.xml

 

Thank You!

Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

Re: Email does not save in quote_address table until after order is placed

Hello @thaiahoygmed08 

 

To save the email in the quote_address table right after the customer clicks the "Next" button during the checkout process in Magento 2

 

Create a Custom Module

Declare the Module

Create the following directory structure:

app/code/Vendor/Module

 

Then, create the registration.php file:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

And create the module.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0"/>
</config>

Create the events.xml file to declare the event you want to observe:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_controller_onepage_save_shipping_method">
        <observer name="vendor_module_save_email_to_quote_address" instance="Vendor\Module\Observer\SaveEmailToQuoteAddress"/>
    </event>
</config>

Implement the Observer

Create the Observer class to handle the event:

<?php


namespace Vendor\Module\Observer;


use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Model\QuoteRepository;
use Magento\Checkout\Model\Session as CheckoutSession;


class SaveEmailToQuoteAddress implements ObserverInterface
{
    protected $quoteRepository;
    protected $checkoutSession;


    public function __construct(
        QuoteRepository $quoteRepository,
        CheckoutSession $checkoutSession
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->checkoutSession = $checkoutSession;
    }


    public function execute(Observer $observer)
    {
        $quote = $this->checkoutSession->getQuote();
        $email = $quote->getCustomerEmail();


        if ($email) {
            foreach ($quote->getAllAddresses() as $address) {
                if ($address->getAddressType() == \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING) {
                    $address->setEmail($email);
                }
            }
            $this->quoteRepository->save($quote);
        }


        return $this;
    }
}

Run all Magento Standard Commands

 

Hope it helps !

If you find our reply helpful, please give us kudos.

 

A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.

 

WebDesk Solution Support Team

Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789

 

 

 

 

 

Thank You,


WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789


Location: 150 King St. W. Toronto, ON M5H 1J9