0

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.

1 Answer 1

0

You can use sales_order_invoice_register that is right after the invoice has been created and before the first save in the database.

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_invoice_register">
        <observer name="event" instance="Module\Name\Observer\InvoiceRegister" />
    </event>
</config>

This event is also triggered if invoices are created automatically.

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.