2

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

3
  • Did you try sales_quote_product_add_after event? Commented Apr 23, 2019 at 11:09
  • For Magento 2.3 frontend use event checkout_cart_product_add_before Commented Apr 23, 2019 at 14:43
  • Did you finally find a fix for this ? Thanks !
    – magentodev
    Commented Jun 12, 2019 at 18:56

2 Answers 2

2

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.

2
  • Your answer looks very in depth. Commented Apr 23, 2019 at 14:35
  • hi @Marius, pls update the code of plugin along with di.xml, Thanks Commented Sep 27, 2019 at 12:37
0

To fix this, you must remove the "start/begin transaction" calls in guest quote management's api/endpoint's function responsible for guest place order.

\Magento\Checkout\Model\GuestPaymentInformationManagement remove these lines enter image description here

I have done this on a few high traffic client's projects without any noticeable regressions. YMMV

The bug is caused by Magento's poor transaction level handling. Its the same reason why little to no logging is available when you see errors like "Failed to rollback" or "Failed to commit" or other "Transaction level" related causes etc.

There are also several other events that dont trigger in cases where certain objects are grouped with the guest order's connection/transaction during place order. Things like sales_invoice_save_commit_after and some others that are likely pretty critical in a number of untested third party extensions.

Here is the actual magento 2 community commit for the fix https://github.com/magento/magento2/commit/43e41fac06019d12e955d521cd9034b5954cb0d9.patch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.