1

I am struggling with a problem regarding placing an order programmatically. I have an extension that creates an order programmatically and does some stuff afterwrds (which is not relevant for my problem here).

Here is the funny thing. When the shipping address destination country is the default location, it works as expected. But if the destination country is Finland for example, I get the exception "The shipping method is missing. Select the shipping method and try again.", even if I definitiely set it, the rates have even been calculated, I checked that. But the order itself won't be submitted.

Does anyone have an idea why? I need help please...

$shippingMethod = $this->_helperData->getConfig('shipping_calculation');
$shippingAddress = $quote->getShippingAddress();

$shippingAddress->setCollectShippingRates(true);
if ($shippingMethod === 'tablerates' ||
   $this->_moduleManager->isDhlInstalled() === false) {
   $shippingAddress->setShippingMethod('tablerate_bestway');
} else {
   $shippingAddress->setShippingMethod('dhlrates_DOM');
}

/*
 * NEXT 2 LINES ONLY FOR TESTING!
 */
$shippingAddress->save();
$quote->setShippingAddress($shippingAddress);

$quote->getPayment()->importData(['method' => 'checkmo']);
$quote->setInventoryProcessed(false);
/*
 * Added setTotalsCollectedFlag(false) for testing
 */

$this->_quoteResource->save($quote->setTotalsCollectedFlag(false)->collectTotals());

//$rates = $shippingAddress->collectShippingRates()
//    ->getGroupedAllShippingRates();

$order = $this->_quoteManagement->submit($quote, $orderData);

3 Answers 3

1

I've faced this problem. My case:

Shipping method was disabled for selected store! Double check it.

0

Here what I've done for this.

Replace $shippingAddress->setCollectShippingRates(true); to $quote->getShippingAddress()->setCollectShippingRates(true);

And also replace $shippingAddress->setShippingMethod('tablerate_bestway'); to $quote->getShippingAddress()->setShippingMethod('tablerate_bestway'); (depending on what shipping method you want to select. It will work.!!

1
  • Save the changes to quote as well.
    – TS Guhan
    Mar 3, 2022 at 15:08
0

Try with below code:

 $shippingQuoteRate = $objectManager->get('\Magento\Quote\Model\Quote\Address\Rate');
    $shippingRateCarrier = 'freeshipping';
    $shippingRateCarrierTitle = 'Freeshipping';
    $shippingRateCode = 'freeshipping_freeshipping';
    $shippingRateMethod = 'freeshipping';
    $shippingRatePrice = '0';
    $shippingRateMethodTitle = 'Free shipping';
    $shippingAddress = $quoteFactory->getShippingAddress();
    $shippingQuoteRate->setCarrier($shippingRateCarrier);
    $shippingQuoteRate->setCarrierTitle($shippingRateCarrierTitle);
    $shippingQuoteRate->setCode($shippingRateCode);
    $shippingQuoteRate->setMethod($shippingRateMethod);
    $shippingQuoteRate->setPrice($shippingRatePrice);
    $shippingQuoteRate->setMethodTitle($shippingRateMethodTitle);
    $shippingAddress->setCollectShippingRates(false)
                    ->collectShippingRates()
                    ->setShippingMethod($shippingRateCode); //shipping method
    $quoteFactory->getShippingAddress()->addShippingRate($shippingQuoteRate);
    $quoteFactory->setPaymentMethod('cashondelivery'); //payment method
    $quoteFactory->setInventoryProcessed(false);
    $quoteFactory->save();

Your Answer

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

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