0

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

2 Answers 2

0

You need to try this code :

 // set shipping method
$shippingAddress=$quote->getShippingAddress();
$shippingAddress->setCollectShippingRates(true)
                ->collectShippingRates()
                ->setShippingMethod('flatrate_flatrate'); //please verify flat rate shipping must be enable
        
$quote->setInventoryProcessed(false); //decrease item stock equal to qty
$quote->save(); //quote save 
5
  • Thanks Dhiren, I am already setting the shipping method but not always to flat rate, if the quote is eligible for free shipping I set it to freeshipping_freeshipping. Should I always set it to flat rate ?
    – Archie
    Commented Feb 25, 2022 at 10:58
  • No, you need to just set the proper shipping method code there. Commented Feb 25, 2022 at 11:39
  • so since I set the shipping method do I also need to create and set the rate as well? For flat rate method the rate is found but for all others (free, table) it isn't, unless I set it, leading to "shipping method is missing".
    – Archie
    Commented Feb 25, 2022 at 12:12
  • First confirm that all are enable and setup properly. rest will work as it is. Commented Feb 25, 2022 at 12:17
  • I would assume they are correctly set because when a quote is eligible for free shipping for example I get the method when I query shipping methods for the quote address. But at the same time there is no rate for this method.
    – Archie
    Commented Feb 25, 2022 at 12:50
0

So I in my case the issue is that I had to first save the quote and then collect rates like so

Magento\Quote\Api\CartRepositoryInterface->save($this->quote);
$this->quote->getShippingAddress()->setCollectShippingRates(true)->collectShippingRates();

Maybe it has to do with the way that I create the quote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.