I am developing a module which programmatically submits an order.
In the store configuration I have enabled both free shipping and table rates shipping methods.
When I get the shipping methods for the quote using a block like below I get all shipping methods (flat rate, free, table) and can retrieve their cost.
$methods = Magento\Quote\Model\ShippingMethodManagement->estimateByExtendedAddress($quote->getId(),
$quote->getShippingAddress());
But when I try to submit the quote unless I add to the quote address rates for each method the order submission fails with the subject error, unless the method is flat rate
. So I use a snippet like below for each quote submit call.
public function setShippingRates(Magento\Quote\Model\Quote $quote)
{
$methods = (get methods as above);
foreach ($methods as $method) {
$methodId = "{$method->getCarrierCode()}_{$method->getMethodCode()}";
$rate = Magento\Quote\Model\Quote\Address\RateFactory->create()
->setCode($methodId)
->setPrice($method->getAmount());
$quote->getShippingAddress()->addShippingRate($rate);
}
}
Is this expected?
Many thanks