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
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);
}
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.