From time to time, you might want to add customers to your Magento store via a script, rather than by hand. This can be especially useful if you’re migrating from an existing solution into a Magento solution.
Setting up a customer with the bare minimum information is easy.
1 2 3 4 5 6 7 8 9 |
$customer = Mage::getModel("customer/customer"); $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); $customer->setStore(Mage::app()->getStore()); $customer->setFirstname("Douglas"); $customer->setLastname("Radburn"); $customer->setPasswordHash(md5("myReallySecurePassword")); $customer->save(); |
A couple of things of note here. Firstly, we need to set the website id, and secondly the store id. We can just grab these straight from Magento. The third thing to note is that Magento (by default) uses MD5 hashes (with salts). If no salt is given, it is blank.
So, that’s it. Simple! So, what if you want to add address information against your customer? Magento tries to keep functionality like this separate, so, you don’t add address details directly into the customer object. Instead, you grab a customer/address model object, and associate to a customer.
1 2 3 4 5 6 7 8 9 10 11 12 |
$address = Mage::getModel("customer/address"); // you need a customer object here, or simply the ID as a string. $address->setCustomerId($customer->getId()); $address->setFirstname($customer->getFirstname()); $address->setLastname($customer->getLastname()); $address->setCountryId("GB"); //Country code here $address->setStreet("A Street"); $address->setPostcode("LS253DP"); $address->setCity("Leeds"); $address->setTelephone("07789 123 456"); $address->save(); |
Image Credit: familymwr
Pingback: [Solved] Adding new customers to the Magento store programmatically • Oriol Moltó
Pingback: Create Magento customer directly to Magento database - Technology