0

I have this event that fires just fine:

<?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_save_after">
        <observer name="remago_addcustomertotree" instance="Remago\Customertree\Observer\Addcustomertotree" />
    </event>
</config>

What I am trying to do is get the customer ID so that it can be added in a different table, when I create it in backend.

This is how I do it:

$customer = $observer->getEvent()->getCustomer();

Now, I am able to get all of his/her data by using

$customer->getName() 

or

$customer->getAddress()

and everything looks ok.

The problem is I can't seem to get anything other than null when I use:

$customer->getId()

or

$customer->getEntityId()

or

$customer->getData('entity_id')

Initially, I thought the problem was that I was using the event

customer_save_before

But changing it to

customer_save_after

had no effects whatsoever.

Any idea?

6
  • Hi. You are trying observer event in M1 or M2? – Raj Jul 30 '18 at 9:09
  • Hi Raj, I'm using M2 – Luca D.R. Jul 30 '18 at 9:10
  • flashed cache after event changing ?..... – Minesh Patel Jul 30 '18 at 9:15
  • I thought I did, but I did a cache:flush and now it's working flawlessly. Thanks Minesh, what a n00b I am! – Luca D.R. Jul 30 '18 at 9:20
  • :P)............... – Minesh Patel Jul 30 '18 at 9:37
0

Try using the customer_save_after_data_object event instead.

In Magento 2.2, it is dispatched here: htdocs/vendor/magento/module-customer/Model/ResourceModel/CustomerRepository.php:239

public function save(
    \Magento\Customer\Api\Data\CustomerInterface $customer, 
    $passwordHash = null
)
{
    // Other customer save logic has been removed for brevity

    $savedCustomer = $this->get($customer->getEmail(), $customer->getWebsiteId());
    $this->eventManager->dispatch(
        'customer_save_after_data_object',
        ['customer_data_object' => $savedCustomer, 'orig_customer_data_object' => $customer]
    );
    return $savedCustomer;
}

See htdocs/vendor/magento/module-quote/etc/adminhtml/events.xml:13 for an implementation example:

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="customer_save_after_data_object">
        <observer name="customer" instance="Magento\Quote\Observer\Backend\CustomerQuoteObserver" />
    </event>
</config>
5
  • Doing a cache:flush (which I thought I did, but it turned out I didn't) solved the problem. But I am in fact still using customer_save_after. Do you think I should use the one you suggested? Why is customer_save_after working if it doesn't exist? – Luca D.R. Jul 30 '18 at 9:22
  • @LucaD.R. customer_save_after doesn't appear to exist in the Magento version I was using to check (2.2.0) - perhaps it does in yours? You didn't specify a complete version after all. – ProcessEight Jul 30 '18 at 9:26
  • @LucaD.R. You should use the event which is most appropriate to your circumstances. Review both, then use the best fit for your extension. – ProcessEight Jul 30 '18 at 9:26
  • I'm using Magento ver. 2.2.5 – Luca D.R. Jul 30 '18 at 9:27
  • @LucaD.R. If your current solution (to use customer_save_after) is working for you, then I'd stick with that. – ProcessEight Jul 30 '18 at 9:31

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.