Magento 2 - The shipping method is missing. Select the shipping method and try again

(Sairaj Gadekar) #1

I am trying to set the Shipping Method Programatically on place Order using Below code . But I am Getting Error

For your Reference I am Using https://github.com/magestat/magento2-split-order/ Code

message: "The shipping method is missing. Select the shipping method and try again."

/**
 * Places an order for a specified cart.
 *
 * @param QuoteManagement $subject
 * @param callable $proceed
 * @param int $cartId
 * @param string $payment
 * @return mixed
 * @throws LocalizedException
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 * @see \Magento\Quote\Api\CartManagementInterface
 */
public function aroundPlaceOrder(QuoteManagement $subject, callable $proceed, $cartId, $payment = null)
{
    $currentQuote = $this->quoteRepository->getActive($cartId);
    // Separate all items in quote into new quotes.
    $quotes = $this->quoteHandler->normalizeQuotes($currentQuote);
    if (empty($quotes)) {
        return $result = array_values([($proceed($cartId, $payment))]);
    }
    // Collect list of data addresses.
    $addresses = $this->quoteHandler->collectAddressesData($currentQuote);
    /** @var \Magento\Sales\Api\Data\OrderInterface[] $orders */
    $orders = [];
    $orderIds = [];
    $orderSrNo = 1;
    $this->checkoutSession->setOrderSrNO($orderSrNo);
    foreach ($quotes as $items) {
        /** @var \Magento\Quote\Model\Quote $split */
        $split = $this->quoteFactory->create();
        // Set all customer definition data.
        $this->quoteHandler->setCustomerData($currentQuote, $split);
        $this->toSaveQuote($split);
        // Map quote items.
        foreach ($items as $item) {
            // Add item by item.
            $item->setId(null);
            $split->addItem($item);
        }
        $this->quoteHandler->populateQuote($quotes, $split, $items, $addresses, $payment);
        // Dispatch event as Magento standard once per each quote split.
        $this->eventManager->dispatch(
            'checkout_submit_before',
            ['quote' => $split]
        );

        $this->helperData->writeLogger('$split->getShippingAddress()->getShippingMethod()');
        $this->helperData->writeLogger(json_encode($split->getShippingAddress()->getShippingMethod()));
        $split->getShippingAddress()->setShippingMethod('freeshipping_freeshipping');
        $split->getShippingAddress()->setCollectShippingRates(true);


        $this->toSaveQuote($split);
        $order = $subject->submit($split);
        $orders[] = $order;
        $orderIds[$order->getId()] = $order->getIncrementId();
        $orderSrNo++;
        $this->checkoutSession->unsOrderSrNO();
        $this->checkoutSession->setOrderSrNO($orderSrNo);

        if (null == $order) {
            throw new LocalizedException(__('Please try to place the order again.'));
        }
    }
    $currentQuote->setIsActive(false);
    $this->toSaveQuote($currentQuote);
    $this->quoteHandler->defineSessions($split, $order, $orderIds);
    $this->eventManager->dispatch(
        'checkout_submit_all_after',
        ['orders' => $orders, 'quote' => $currentQuote]
    );
    return $this->getOrderKeys($orderIds);
}
/**
 * Save quote
 *
 * @param \Magento\Quote\Api\Data\CartInterface $quote
 * @return \MyModule\SplitOrder\Plugin\SplitQuote
 */
private function toSaveQuote($quote)
{
    $this->quoteRepository->save($quote);
    return $this;
}
/**
 * @param array $orderIds
 * @return array
 */
private function getOrderKeys($orderIds)
{
    $orderValues = [];
    foreach (array_keys($orderIds) as $orderKey) {
        $orderValues[] = (string) $orderKey;
    }
    return array_values($orderValues);
}
0 Likes