Custom Events in Magento

Custom Events in Magento

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Custom Events in Magento

Adding Custom Events in Magento

Are you looking to add custom events in your Magento store? If so, you’re not alone—many developers need more flexibility than what’s built-in. In this post, we’ll walk through the process of creating a custom event like done_some_thing and using it to show special discounts on both cart and checkout pages.

Understanding Custom Events in Magento

In Magento, events are a powerful way for modules to communicate with each other. They allow you to trigger specific actions when certain conditions are met—like when a customer applies a discount code or proceeds through checkout.

To create your own event, you’ll need to use the Event Manager and define both an observer and an event object.

Creating Your Custom Event

Let’s say we want to create an event called done_some_thing. Here’s how:

  1. Create the Observer

First, create a new observer file in your custom module folder (e.g., app/code/Vendor/Module/etc/events.xml). The XML should look like this:

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

Notice that we’ve defined a new event named done_some_thing and an observer called vendor_module_discount_observer.

  1. Create the Observer Class

Next, create the actual observer class (DiscountObserver.php) in your module:

class DiscountObserver implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Your custom logic here
        return $this;
    }
}

Triggering the Event

Now, let’s say you want to trigger this event when a customer applies a certain discount code. You can do this in your discount model:

public function applyDiscount($code)
{
    if ($code === 'SPECIAL_DISCOUNT') {
        $this->eventManager->dispatch('done_some_thing');
    }

    // Rest of the method...
}

Showing Discount on Cart and Checkout

To show this special discount on both cart and checkout pages, you can use Magento’s layout XML files. For instance, in your custom module’s view/frontend/layout folder:

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.cart.totals" before="-">
            <block class="Magento\Checkout\Block\Cart\Total" as="special_discount">
                <action method="setLabel" translate="value">
                    <argument name="value" xsi:type="string">Special Discount:</argument>
                </action>
                <action method="setValue">
                    <argument name="value" xsi:type="number">10.5</argument>
                </action>
            </block>
        </referenceBlock>
    </body>
</page>

Conclusion

Adding custom events to your Magento store is a powerful way to extend functionality and create dynamic, customer-focused experiences.

Remember, the key is to use the Event Manager properly, defining both event objects and observers in the right places. With this guide, you should now be able to create custom events like done_some_thing and leverage them for special promotions or discounts.

For more advanced Magento customization techniques, check out Laravel Company’s Magento Development Services. We offer expert consultation and implementation for all your Magento needs.

Checkout Extensions Configuration Customers

Our network