Question

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!!

Was it helpful?

Solution

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();

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top