cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 saving customer through observer in customer_register_success is saving twice

SOLVED

Magento 2 saving customer through observer in customer_register_success is saving twice

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? 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 saving customer through observer in customer_register_success is saving twice

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);

View solution in original post

2 REPLIES 2

Re: Magento 2 saving customer through observer in customer_register_success is saving twice

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);

Re: Magento 2 saving customer through observer in customer_register_success is saving twice

Brilliant! Exactly what I was looking for.