1

I have a order to create an order in magento 2. But the order it create is with the date when i run the script. I don't want to save the current date for the order. I want to set a certain date to the that order.

2 Answers 2

0

A tricky wat, you set your desired time to created_at field

Magento 2 has a method setCreatedAt() and using this method can set Order created time.

So add below code after the end of your order creation code and run update() of this code.

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

public function update()
{
    $order = $this->orderRepository->get(1); // 1 is  order id
    $order->setCreatedAt($createdAt); // set time
    $this->orderRepository->save($order);   

} 
1
  • No it didn't worked.
    – Avesh Naik
    May 31, 2019 at 7:22
0

Finally i found the solution.

My question was not fully complete. Sorry about that. I wanted the order date to be on certain date. The order is not yet created. I am creating the order using my custom code. To create an order i need to create Quote. So if i change the quote create and update date then i reflects to order date too.

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.