On checkout page by default my saved addresses are rendered. How I'll set a custom address as shipping address instead of addresses shown from my account?
I've added an event to observe checkout page load
<?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="controller_action_predispatch_checkout_index_index">
<observer name="vendorcheckout_before_dispatch" instance="Vendor\Module\Observer\Dispatch" />
</event>
</config>
and here is my observer code
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class Dispatch implements ObserverInterface
{
public function __construct(
\Magento\Checkout\Model\Session $session,
\Magento\Quote\Api\Data\AddressInterface $address,
\Magento\Checkout\Api\ShippingInformationManagementInterface $shippingInformationManagement,
\Magento\Checkout\Api\Data\ShippingInformationInterface $shippingInformation
)
{
$this->session = $session;
$this->address = $address;
$this->shippingInformationManagement = $shippingInformationManagement;
$this->shippingInformation = $shippingInformation;
}
/**
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$address = $this->address
->setFirstname('john')
->setLastname('cena')
->setStreet('xxxx')
->setCity('Delhi')
->setCountryId('US')
->setRegionId(26)
->setRegion('Kansas')
->setPostcode('6666')
->setTelephone('444')
->setFax('3333')
->setSaveInAddressBook(0)
->setSameAsBilling(0);
if ($this->session->getQuote()) {
$cartId = $this->session->getQuote()->getId();
if ($cartId) {
$add = $this->shippingInformation->setShippingAddress($address);
$this->shippingInformationManagement->saveAddressInformation($cartId, $add);
}
}
}
}
Above code is throwing
Exception #0 (Magento\Framework\Exception\NoSuchEntityException): Carrier with such method not found: ,
Just to test manually I've added $carrierCode
and $methodCode
as flatrate
in class module-checkout/Model/ShippingInformationManagement
This is adding shipping address properly to quote_address
table. Though on checkout still my account addresses are shown instead of this custom shipping address from quote_address table.
Can someone guide me with following:
1. How to fix exception Carrier with such method not found
?
2. Do I need to update this address somewhere else also to render it on checkout page and save as shipping address with order?
$quote->getShippingAddress()->addData($addressArray);
?