3

I have an order which has the grand total that I need to fix because one of my products prices is incorrect, to do that I need to change the order item price and recollect order total, is there a way I can do this programmatically?

Here's how I load my order:

$order = $this->objectManager->create('\Magento\Sales\Model\Order')->load($orderId);

1 Answer 1

2

The solution will like below

$orderId = 'put_your_order_id';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quoteToOrder = $objectManager->create('Magento\Quote\Model\Quote\Item\ToOrderItem');
$order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);
$quote = $objectManager->create('\Magento\Quote\Model\Quote')->load($order->getQuoteId());

First we need to update the order quote.

foreach ($items as $quoteItem) {
    $origOrderItem = $order->getItemByQuoteItemId($quoteItem->getId());
    $orderItemId = $origOrderItem->getItemId();
    //update quote item according your need 
}
$quote->collectTotals();
$quote->save();

Then update the order

foreach ($items as $quoteItem) {
    $orderItem = $quoteToOrder->convert($quoteItem);
    $origOrderItemNew = $order->getItemByQuoteItemId($quoteItem->getId());

    if ($origOrderItemNew) {
        $origOrderItemNew->addData($orderItem->getData());
    } else {
        if ($quoteItem->getParentItem()) {
            $orderItem->setParentItem(
                $order->getItemByQuoteItemId($orderItem->getParentItem()->getId())
            );
        }
        $order->addItem($orderItem);
    }
}
$order->setSubtotal($quote->getSubtotal())
      ->setBaseSubtotal($quote->getBaseSubtotal())
      ->setGrandTotal($quote->getGrandTotal())
      ->setBaseGrandTotal($quote->getBaseGrandTotal());
$quote->save();
$order->save();
4
  • please share all modules code. I am beginner in magento
    – Gagan
    Commented Sep 3, 2020 at 8:03
  • Hi @Gagan sorry we don't have the module code for this question, you can please check our answer. Commented Sep 3, 2020 at 8:47
  • @MagetopE-commerce where is $items defined?
    – Gaz Smith
    Commented Jul 15, 2021 at 11:11
  • For quote items: $items = $quote->getAllItems(); and for order items: $items = $order->getAllItems(); Commented Oct 28, 2021 at 7:26

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.