1

What is the best way to identify a new customer when using an afterSave Plugin?

Or should I use a different approach to do something with new customers?

1 Answer 1

3

Whenever a customer registered through the frontend form Magento trigger the customer_register_success event:

File: vendor/magento/module-customer/Controller/Account/CreatePost.php

    $this->_eventManager->dispatch(
        'customer_register_success',
        ['account_controller' => $this, 'customer' => $customer]
    );

Or either you can find the newly created customers by comparing the saved customer and previous customer data by observing customer_save_after_data_object event.

File: vendor\magento\module-customer\Model\ResourceModel\CustomerRepository.php

$this->eventManager->dispatch(
    'customer_save_after_data_object',
    [
        'customer_data_object' => $savedCustomer,
        'orig_customer_data_object' => $prevCustomerData,
        'delegate_data' => $delegatedNewOperation
            ? $delegatedNewOperation->getAdditionalData() : []
    ]
);
1
  • Thanks MageAj. The customer_save_after_data_object was exactly what I was looking for. Now I just created an observer for it and added $isNewCustomer = $observer->getData('orig_customer_data_object') === null; Commented Mar 1, 2019 at 0:54

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.