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.

1
  • I am amazed that no one from the community reacts so far. Commented Mar 21, 2018 at 9:19

2 Answers 2

3

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

6
  • I need more help Commented Mar 21, 2018 at 12:15
  • so? what do you need? Commented Mar 21, 2018 at 12:17
  • I have a new field in customer register form named referral_code i just want to get this field data in my UpdateCustomerMeta observer how can i get the value of the field "referral_code" Commented Mar 21, 2018 at 12:19
  • 1
    I think the best would be to create a new question for that Commented Mar 21, 2018 at 12:27
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

2
  • frontend is working fine Commented Mar 21, 2018 at 10:11
  • yes you are right but i just want this for frontend Commented Mar 21, 2018 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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