Confirm new customer programmatically in Magento

A recent head-scratcher for a client cropped up when their customers were receiving repeated confirmation emails on signing into their accounts.

Background


They had an interesting set up – customers couldn’t create accounts through the website, instead new customers were created in their CRM, and then uploaded to a CSV on the site nightly – that CSV was then processed by a script in Magento which would review each customer and create them a new account, then email them a welcome email with their new account details.

The configuration settings in Magento didn’t require customers to confirm their email addresses, but even though the script looked like it was setting the customers as confirmed, they weren’t being confirmed when created in the system.

There are two things to bear in mind :

1. The customer entity has a confirmation attribute which has a value only if the customer hasn’t been confirmed. Since creating a customer programmatically over-rides what the configuration may say about confirmation, therefore, one would need to set the customer as confirmed, passing a null value, to mark them as confirmed in the database.

2. It’s necessary to save the new customer before and after doing this – so having set all the other information about the new customer, one would then save, set them as confirmed, then save again, like so :

$newCustomer->save();
$newCustomer->setConfirmation(null);
$newCustomer->save();

And that’s it. No more confirmation issues. Hope that helps someone.