Magento 2: Why sales_order_save_commit_after Does Not Fire When User is Not Logged In

event-observermagento2orders

I am using an observer to handle some logic when a new order is created or updated. The observer listen to sales_order_save_commit_after event, but I noticed that when the user is not logged in this event does not fire. What is the reason that it does not trigger in this case? Can it be substituted with some other event?

P.s. I checked some other events, like sales_order_save_after but the items are still not saved when it triggers and checkout_onepage_controller_success_action does not work in case of creating the order from admin.

Cases when events are triggered

Best Answer

In the Temando_Shipping module there is the method Temando\Shipping\Plugin\Sales\OrderRepositoryPlugin::afterSave.
In the comment of this method there is this

/**
 * Manifest order at Temando platform.
 *
 * Observers don't work:
 * - `sales_order_save_commit_after` is no longer triggered in guest checkout
 * - `sales_order_save_after` does not have related entities (addresses) persisted yet
 *
 * Other promising events like `sales_order_place_after`, `checkout_submit_all_after`,
 * `sales_model_service_quote_submit_success` are
 * - triggered before the order was saved or
 * - not triggered at all in multi address checkout or some payment providers'
 *   custom checkout implementations (paypal express, sagepay, …).
 *
 * @param SalesOrderRepositoryInterface $subject
 * @param OrderInterface|\Magento\Sales\Model\Order $salesOrder
 * @return OrderInterface
 */

I don't have a response to "why?" I'm just stating that it's by design.

As a bonus checkout_onepage_controller_success_action is not dispatched in admin because that's event is not triggered at all in the backend.

Maybe you can use a plugin similar to what the Temando module uses.
An afterSave plugin on the Order repository class.

Related Topic