- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2019
01:18 PM
03-20-2019
01:18 PM
I have a simple observer that sets the customer group id to a custom group id on customer_register_success.
$customer = $observer->getEvent()->getCustomer(); $customer->setGroupId($customGroupId); $this->_customerRepositoryInterface->save($customer);
The problem is because I'm calling the save function in observer its firing the it twice.
The issue with this is the newsletter subscribe function is getting fired twice with different results setting it the status to 1 and and the next one to 3. And if I call setGroupId and don't save and continue on it doesn't change the groupId of the customer.
How do I got about saving the groupId on account creation?
Solved! Go to Solution.
Labels:
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019
09:35 AM
03-21-2019
09:35 AM
Try to set is_subscribed as well
$customer = $observer->getEvent()->getCustomer();
$isSubscribed = $observer->getEvent()->getAccountController()->getRequest()->getParam('is_subscribed');
$customer->getExtensionAttributes()->setIsSubscribed($isSubscribed);
$customer->setGroupId($customGroupId);
$this->_customerRepositoryInterface->save($customer);
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2019
09:35 AM
03-21-2019
09:35 AM
Try to set is_subscribed as well
$customer = $observer->getEvent()->getCustomer();
$isSubscribed = $observer->getEvent()->getAccountController()->getRequest()->getParam('is_subscribed');
$customer->getExtensionAttributes()->setIsSubscribed($isSubscribed);
$customer->setGroupId($customGroupId);
$this->_customerRepositoryInterface->save($customer);
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2019
02:23 AM
03-28-2019
02:23 AM
Re: Magento 2 saving customer through observer in customer_register_success is saving twice
Brilliant! Exactly what I was looking for.