3

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.

  • I am amazed that no one from the community reacts so far. – Abdullah Shahid Mar 21 '18 at 9:19
2

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

| improve this answer | |
0

You have to change the path of your event.

Customer registration is the common event for frontend and backend.

So your event path should be app/code/vendor/module/etc/events.xml

| improve this answer | |
  • frontend is working fine – Raul Sanchez Mar 21 '18 at 10:11
  • yes you are right but i just want this for frontend – Abdullah Shahid Mar 21 '18 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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