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?
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() : []
]
);
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