Try this,
In your controller add the following code
<?php
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;
parent::__construct($context);
}
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');
$order->save();
}
echo "success";
} catch (\Exception $e) {
error_log($e->getMessage());
}
}}
hope this may help and let me know if it works or not.
Magento\Sales\Model\Order\Item
and can save the data you want.