Skip to content

Setting shipping and billing address for guest customer from custom page

I would like to set billing and shipping address from my custom page (only for guest customers).My checkout page has been changed and overrided and the shipping and payment steps are changed(no longer /#shipment and /#payment URls : only /checkout). I have already created my form and my controller under the URL /checkout/account/guest.
I tried to set the data comes from my form to the quote address but it didn’t work: when i submit my form in quote_address table the billing address is saved correctly but the shipping address after some time the data is removed
quote_address table
But in checkout page, the addresses are not displayed and when i debug the quote the shipping and billing addresses are empty.

Here is my controller code (for setting addresses in quote):

public function execute()
{

    if (!$this->_formKeyValidator->validate($this->getRequest())) {
        $this->messageManager->addErrorMessage(__('Invalid form key.'));
        return $this->_redirect('*/*/');
    }

    $postData = $this->getRequest()->getPostValue();


    try {
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $quoteRepository = $objectManager->get("MagentoQuoteApiCartRepositoryInterface");
        // Get current quote
        $quote = $quoteRepository->getActive($this->_checkoutSession->getQuote()->getId()) ;

        $quote->setGuestEmail($postData['email']);

        $quote->getBillingAddress()->addData([
            'email' => $postData['email'],
            'prefix' => $postData['prefix'],
            'firstname' => $postData['firstname'],
            'lastname' => $postData['lastname'],
            'company' => $postData['company'],
            'street' => $postData['street'],
            'postcode' => $postData['postcode'],
            'city' => $postData['city'],
            'country_id' => $postData['country_id'],
            'telephone' => $postData['telephone']

        ]);
        

        $quote->getShippingAddress()->addData([
            'email' => $postData['email'],
            'prefix' => $postData['prefix'],
            'firstname' => $postData['firstname'],
            'lastname' => $postData['lastname'],
            'company' => $postData['company'],
            'street' => $postData['street'],
            'postcode' => $postData['postcode'],
            'city' => $postData['city'],
            'country_id' => $postData['country_id'],
            'telephone' => $postData['telephone'],
            'same_as_billing' => true

        ]);

        
        $quoteRepository->save($quote);


        $resultRedirect = $this->_redirectFactory->create();
        return $resultRedirect->setPath('checkout');
    } catch (LocalizedException $e) {
        $this->messageManager->addErrorMessage(__('An error occurred while saving the address.'));
        return $this->_redirect('*/*/');
    }

So my question is how can i set the addresses from my custom form to be displayed in the checkout page when guest customer submit the form ?

Thanks in advance