Magento 2.2.2 – customer_register_success Not Working

magento2.2.2

I want to get customer id after new customer register successfully in Magento 2.2.2.
In app/code/vendor/module/etc/frontend/events.xml-> I added this code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_register_success">
        <observer name="update_customer_referal_code" instance="vendor\module\Observer\UpdateCustomerMeta" />
    </event>
</config>

and in app/code/vendor/module/Observer/UpdateCustomerMeta.php -> I have added this

<?php

namespace vendor\module\Observer;

use Magento\Framework\Event\ObserverInterface;

class UpdateCustomerMeta implements ObserverInterface
{

    protected $_customerRepositoryInterface;

    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ) {
        $this->_customerRepositoryInterface = $customerRepositoryInterface;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        print_r($customer->getId());
        echo "if this string shows it means observer works";
        exit();
    }
}

but when I register a user it directly goes to customer panel after registering anyone please tell what mistake I am making.

Best Answer

I have tested your code in a Magento2.2 installation and it works

enter image description here

I assume you have a well registered working module named vendor_module

So I suspect maybe there is a typo when you say vendor\module, are your module name & folders really lowercase?

If there are no typo error, have you run both bin/magento setup:upgrade & bin/magento setup:di:compile commands?

If we've reached here, then the only explanation is you have some 3rd party extension installed, which is causing customer_register_success event not to be fired

Related Topic