Magento 2 event before order is placed

Magento 2 event before order is placed

July 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 event before order is placed

Handling Magento 2 Events Before Order Placement

When working with Magento 2’s checkout process, you might often need to execute custom logic between when the customer clicks “Place Order” and when the order is actually placed in the system. This requires understanding the correct event to use at that specific point.

Identifying the Right Event

The key here lies in recognizing that the order placement isn’t a single, instantaneous action but rather a sequence of events. When you click “Place Order”, Magento initiates multiple processes before finalizing the order:

  1. Shipping and payment method validation: The system first validates whether the chosen shipping and payment methods are indeed available for the given cart.

  2. Order data preparation: All necessary order data is prepared from the cart contents, customer information, etc.

  3. Actual order placement: Once everything checks out, Magento finally creates the order in the database.

Using the checkout_onepage_save_order Event

Given this sequence, you wouldn’t want to use an event that fires before all validations occur or after the actual order placement. The ideal event for your needs is the checkout_onepage_save_order event, which is dispatched right between validation and final order creation.

Here’s a simple example of how to observe this event:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    // Your custom logic here using the order object

    return $this;
}

To implement this in your Magento 2 extension, you can create an observer class following a typical structure:

namespace MagetnoCompany\YourModule\Observer;

use Magento\Framework\Event\ObserverInterface;

class SaveOrder implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Your custom logic here

        return $this;
    }
}

You would then need to configure the observer in your module’s etc/events.xml file:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_save_order">
        <observer instance="MagetnoCompany\YourModule\Observer\SaveOrder" />
    </event>
</config>

Best Practices and Further Reading

Remember, when observing this event, you’re handling a critical part of the checkout process. Ensure your logic is efficient and avoids any potential performance issues.

For more advanced use-cases or if you need to interact with other models besides the order object, consider using Magento’s dependency injection for better code organization.

To learn more about Magento 2 events and observers in general, I recommend checking out Laravel Company’s guide on Magento 2 event management for a comprehensive walkthrough.

Conclusion

Understanding the checkout_onepage_save_order event is key to capturing the right moment during order placement in Magento 2. By observing this event, you can ensure your custom logic executes exactly when needed—after validations and before the actual order creation process begins.

This approach provides a reliable way to extend Magento’s core functionality without interrupting its built-in flow or introducing potential conflicts with other extensions. It’s a common practice used by many experienced Magento developers.

Remember to test your observer thoroughly to ensure it behaves as expected under different scenarios, particularly during edge cases like validation failures or system errors. Happy coding!

Magento 2 Checkout Database Security

Our network