4

I have to update some sales order parameters

$order = $this->orderInterface->loadByIncrementId('000000009');
 $order->setStatus($status)
       ->setState($status)
       ->save();

Here save() is @deprecated and also if try to save, it does not save.

I tried:

/**
*  @var OrderStatusApiInterface
*/
$this->orderRepositoryInterface->save($order);

It changes all the other data also is completely false.

2 Answers 2

3

you can use OrderRepositoryInterface to load the order and ResourceModel to save order. I tested follow code and it's works fine at my local Magento.

public function __construct(
    \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
    \Magento\Sales\Model\ResourceModel\Order $orderResourceModel
) {
    $this->orderResourceModel = $orderResourceModel;
    $this->orderRepository = $orderRepository;
}

public function execute()
{
    $order = $this->orderRepository->get(1);
    $order->setCustomerEmail('[email protected]');
    $this->orderResourceModel->save($order);
}
1
  • saved my time +1 Jan 22, 2019 at 13:27
0
class Index extends \Magento\Backend\App\Action
{    
  protected $resultJsonFactory;  
  protected $_orderRepository ;

   public function __construct(
     \Magento\Backend\App\Action\Context $context,
     \Magento\Sales\Model\Order $orderRepository,
     \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
   ) {
   parent::__construct($context);
   $this->resultJsonFactory = $resultJsonFactory;
   $this->_orderRepository = $orderRepository;
} 

  public function execute()
  {
    $email = $this->getRequest()->getParam('email');
    $orderId = $this->getRequest()->getParam('OrderId');
    $_order = $this->_orderRepository->load($orderId); // it order is not order increment id
    if($_order->getId()){
        $_order->setCustomerEmail($email);
        $_order->save();
         echo "Saved";
    }else{
        echo "Error:Not Saved";
    }
  }

}

1
  • Update customer order data through code in magento2.x
    – Lokesh Das
    Jun 21, 2018 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.