In Magento 2, we can update an order data (a data in sales_order) using Magento Sales OrderRepository .

My questions is:

How we can update the sales_order_item data (the qty_invoiced and qty_shipped using order_id?

Can anyone give me at least a way on how to do it or where to start?

  • You can load the model Magento\Sales\Model\Order\Item and can save the data you want. Commented Feb 21, 2019 at 8:32
  • I need to update data using order_id, can you give an example on how to implement it?
    – fmsthird
    Commented Feb 21, 2019 at 8:33
  • 1
    @magefms Updated the answer as per the requirement. Please check +1 for the effort this question made me to take. Commented Feb 21, 2019 at 11:46

1 Answer 1


Try this, In your controller add the following code

 namespace Vendor\Module\Controller\Orders;                       
 class SetOrders extends \Magento\Framework\App\Action\Action
  protected $orderItemRepo;
  protected $resultPageFactory;
  protected $itemFactory;
  public function __construct(
    \Magento\Sales\Model\Order\ItemFactory $itemFactory,
    \Magento\Sales\Api\OrderItemRepositoryInterface $orderItem,
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
    $this->itemFactory = $itemFactory;
    $this->orderItemRepo = $orderItem;
    $this->resultPageFactory = $resultPageFactory;
public function execute()
    try {
        //$itemId = 6;
        $orderId = 5;
        $order = $this->itemFactory->create()->getCollection()->addFieldToFilter('order_id', $orderId);
        foreach ($order as $items) {
            $itemId =  $items->getItemId();
            $order = $this->orderItemRepo->get($itemId);
            $order->setAdditionalData('this is custom field');
        echo "success";
    } catch (\Exception $e) {

hope this may help and let me know if it works or not.

  • I'll check and inform you if it works
    – fmsthird
    Commented Feb 21, 2019 at 8:54
  • "Fatal Error: 'Uncaught Error: Call to a member function setCustomField()
    – fmsthird
    Commented Feb 21, 2019 at 9:04
  • wait I'll try that
    – fmsthird
    Commented Feb 21, 2019 at 9:10
  • Replace this $order->setCustomField(); with $order->setQtyOrdered(10); This will update the ordered items. Commented Feb 21, 2019 at 9:12
  • the same error I got after changing
    – fmsthird
    Commented Feb 21, 2019 at 9:14

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.