Normally, Magento will set the Order status to Complete
once invoice is created but I want to set it to processing
for some reason.
I have tried using these events:
- sales_order_invoice_pay
- sales_order_invoice_register
- sales_order_invoice_save_after
but still no luck. When I log the Order status after saving, it logs the correct updated status but the order still proceeds to complete (both UI and database).
Here's what I did so far:
/**
* @param LoggerInterface $logger
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
LoggerInterface $logger,
OrderRepositoryInterface $orderRepository
) {
$this->logger = $logger;
$this->orderRepository = $orderRepository;
}
/**
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$invoice = $observer->getEvent()->getInvoice();
$order = $this->orderRepository->get($invoice->getOrderId());
$this->logger->info('i am here!'); // this works
$this->logger->info($invoice->getOrderId()); // this works
$this->logger->info($order->getState()); // state new
$this->logger->info($order->getStatus()); // status pending
$order->setStatus(Order::STATE_PROCESSING)
->setState(Order::STATE_PROCESSING);
$this->orderRepository->save($order);
$this->logger->info($order->getState()); // state updated processing
$this->logger->info($order->getStatus()); // status updated processing
}
I'm looking for a solution that does not use raw query.
Interface: Magento\Sales\Api\OrderRepositoryInterface
I found this link How to update Order data in Magento 2?
but still it's not working.