Skip to content

Programmatically change address details on checkout

I’m trying to update the address details when the Payment method is changed. Depending on what payment method is selected, it will change an address that will be pre-configured. This is used for quick checkout for internal processes only, it is also IP restricted. However, this code IS updating the Quote tables in the database, however it isn’t updating the details on the checkout page. How can those details be updated so that when toggling through the payment methods the frontend updates these details.

use MagentoFrameworkEventObserverInterface;

class AddQuickCheckoutAddressObserver implements ObserverInterface
{
    protected $_customerSession;
    protected $objectManager;

    protected $quoteRepository;

    public function __construct(
        MagentoCustomerModelSessionFactory $customerSession,
        MagentoQuoteModelQuoteRepository $quoteRepository
    ) {
        $this->_customerSession = $customerSession->create();
        $this->objectManager = MagentoFrameworkAppObjectManager::getInstance();
        $this->quoteRepository = $quoteRepository;
    }

    public function execute(MagentoFrameworkEventObserver $observer)
    {

        $session = $this->objectManager->create(MagentoCheckoutModelSession::class);

        $quote = $session->getQuote();

        $cartId = $session->getQuote()->getId();

        $quote->getBillingAddress()->setCountryId('DE');
        $quote->getBillingAddress()->setPostcode('32825');
        $quote->getBillingAddress()->setCity('Berlin');
        $quote->getBillingAddress()->setStreet('Berlinstreet 1');
        $quote->getBillingAddress()->setTelephone('0123456789');
        $quote->getBillingAddress()->setFirstName('John');
        $quote->getBillingAddress()->setLastName('Young');



        $quote->getShippingAddress()->setCountryId('DE');
        $quote->getShippingAddress()->setPostcode('32825');
        $quote->getShippingAddress()->setCity('Berlin');
        $quote->getShippingAddress()->setStreet('Berlinstreet 1');
        $quote->getShippingAddress()->setTelephone('5555');
        $quote->getShippingAddress()->setFirstName('John');
        $quote->getShippingAddress()->setLastName('Young');

        $quote->save();
    }
}