Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top