2

I want to edit some data's of ordered items like weight, price and some more.

For that, I got the ordered item collection for the specific order.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderItemId = '3';
$orderItem = $objectManager->create('\Magento\Sales\Api\OrderItemRepositoryInterface')->get($orderItemId);
$orderItems = $orderItem->getData()

So $orderItems has the ordered item collection.

And then I have tried to edit the ordered items like below.

foreach ( $orderItems->getData() as $val ) {
    $val->setWeight(1)->save();
}

But the weight not gets updated.

Full Code:

$orderId = $_GET['id'];
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderId);

// Edit the order items data
foreach ($order->getAllItems() as $key => $value) {
    $orderItemId = $value->getData('item_id');
    $orderItem = $objectManager->create('\Magento\Sales\Api\OrderItemRepositoryInterface')->get($orderItemId);
    $orderItems = $orderItem->getData();
    foreach ( $orderItems as $val ) {
        $val->setWeight(1)->save();
    }
}
$orderResourceModel->save($order);

I've just referred this link here. But I'm not having a clear idea about the orderquote.

I'm using magento 2.3 version.

Please help me. I am a novice in magento and I am stuck at this point. Thank you in advance!!

2 Answers 2

4

Use below method to update order item data, Note: using objectManager directly is not recommended.

protected $_orderItems;

public function __construct(
    \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $orderItems
)
{
    $this->_orderItems = $orderItems;
}

public function execute()
{
    $orderItem = $this->_orderItems->create()->addFieldToFilter( 'item_id', $orderItemId )->getFirstItem();
    $orderItem->setWeight(1);
    $orderItem->save();
}

For testing purpose use

use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrapp = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrapp->getObjectManager();
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');

$orderItem = $objectManager->get('\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory')->create()->addFieldToFilter( 'item_id', $orderItemId )->getFirstItem();
$orderItem->setWeight('2')->save();
9
  • Thanks Ranganathan. Will try this method and let you know.
    – Ramya
    Jul 19, 2019 at 5:04
  • Can I use the order item collection factory via object manager. $orderItem = $objectManager->create('\Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory')->addFieldToFilter( 'item_id', $orderItemId )->getFirstItem();
    – Ramya
    Jul 19, 2019 at 5:14
  • Where do you want to implement this code? Jul 19, 2019 at 5:15
  • For my testing purpose, I'm writing the code on magento root folder
    – Ramya
    Jul 19, 2019 at 5:16
  • Yes you can use objectmanager here only...... Jul 19, 2019 at 5:17
0

You can update or edit the existing order by following

$orderId = '21';
$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());

Update the order quote.

$items = $quote->getQuote()->getAllVisibleItems();

foreach ($items as $quoteItem) 
{
    $origOrderItem = $order->getItemByQuoteItemId($quoteItem->getId());
    $orderItemId = $origOrderItem->getItemId();

    //update quote item according your need 
    $quoteItem->setWeight();
    $quoteItem->setQty();
    ....
}
$quote->collectTotals();
$quote->save();

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();
9
  • But the question is how to update order items......Not how to update (order)/(quote items)... Jul 19, 2019 at 5:02
  • you can edit order item as well, look at "update quote item according your need"
    – Hiren
    Jul 19, 2019 at 5:05
  • I've just referred this link here. But I'm not having a clear idea about the orderquote.
    – Ramya
    Jul 19, 2019 at 5:05
  • But you didn't declared the $items variable..What the items variable has? Jul 19, 2019 at 5:07
  • You're right Ranganathan. That was my doubt while I'm referring that above link.
    – Ramya
    Jul 19, 2019 at 5:13

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.