create an observer for customer logged in magento

create an observer for customer logged in magento

October 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
create an observer for customer logged in magento

How to Create an Observer for Customer Logged In Magento

In Magento, observers play a crucial role in handling events within the system. They allow you to listen for specific actions like customer login and then perform custom logic in response. Let’s dive into how to create an observer for the “customer logged in” event.

Understanding Observers in Magento

An observer is a PHP class that is notified when a particular event occurs, allowing it to take appropriate action. The observer configuration defines which events it listens for and what methods should be executed when those events occur.

For example, the Magento system contains many built-in observers like checkout_cart_save_before, catalog_product_save_after, etc., each handling specific business logic related to their respective events.

Creating an Observer for Customer Logged In

To create an observer for the “customer logged in” event, follow these steps:

  1. Create a module: First, you need to create your own module where the observer will be placed. If you haven’t set up any modules yet, check out our guide on creating Magento 2 modules for detailed instructions.

    Assume we have created a module called Vi_Cartproduct.

  2. Create the observer class: Inside your module’s Observer directory, create a PHP class that will handle the event. For our example, let’s create a file named CustomerLogin.php:

<?php

namespace Vi\Cartproduct\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class CustomerLogin implements ObserverInterface
{
    public function execute(Observer $observer)
    {
        // Your custom logic here
        // For example, you can add a product to the cart or send notifications
    }
}

The execute method is where you define what should happen when the “customer logged in” event occurs.

  1. Configure the observer: Open your module’s etc/events.xml file and include the following configuration:
<events>
  <customer_login> <!-- This is the identifier of the event we want to catch -->
    <observers>
      <Vi_Cartproduct> <!-- This is the identifier of the event handler -->
        <type>singleton</type> <!-- Class method call type; valid are model, object and singleton -->
        <class>cartproduct/observer</class> <!-- Observers class alias -->
        <method>customerLogin</method>  <!-- Observer's method to be called -->
      </Vi_Cartproduct>
    </observers>
  </customer_login>
</events>

The customer_login identifier specifies the event that triggers our observer. The Vi_Cartproduct identifier is the name of our observer class.

  1. Test your setup: Clear your cache and run a customer login action in the frontend. Your custom logic should now execute as defined in the execute method of the CustomerLogin class inside the Observer directory.

Remember to replace any placeholders with your actual code and variables according to your specific requirements.

Troubleshooting Common Issues

If you’re facing issues, double-check that:

  • You have enabled your module.
  • The event identifier matches exactly (customer_login, not customer_logged_in).
  • Your observer class is correctly named and placed in the right directory.
  • The cache is cleared after making changes to configuration files.

Conclusion

Creating an observer for a customer logged-in event allows you to extend Magento’s functionality without modifying core code. This approach keeps your customizations portable between versions and maintains the ability to update safely while preserving your custom logic.

Observers are powerful tools that enable you to respond to Magento events in a clean, decoupled manner. By building your own observers, you can tailor the Magento experience to meet specific business needs.

Don’t hesitate to explore more about Magento’s event-driven architecture and how it allows for flexible customizations without touching core files.

Happy coding!

Magento 2 Checkout Catalog Extensions

Our network