3

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.

1

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('new@amasty.com');
    $this->orderResourceModel->save($order);
}
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";
    }
  }

}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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