Magento Development

Here you will learn all the ins and outs of adding a new customer to your Magento store. However, this can be done by creating a new customer through a sign-up form or via admin interface. But this might take too much of your time. On the other hand, developers love to add things programmatically. If you wish to have a bunch of customers assigned to different groups and from different countries, you can do this better writing a code.

Let’s see how we can add customers to your Magento store. Just follow the below steps:

For Beginners – Let’s add a customer with some basic information.

$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
$customer = Mage::getModel("customer/customer");
$customer   ->setWebsiteId($websiteId)
            ->setStore($store)
            ->setFirstname('Alen')
            ->setLastname('Nyle')
            ->setEmail('abc@xyz.com')
            ->setPassword('newpassword');
try{
    $customer->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

Result:
Magento Development

The above code adds a customer with only first and last name, email and password set. The above code illustrates an example meant for beginners to add some basic information about the customer to the Magento estore. But you can do even more. You can add middle name, assign the customer to a specific customer group and even can add prefixes and suffixes to the customer name.

You can refer the below code to add middle name, suffix or prefix.

$customer   ->setWebsiteId($websiteId)
            ->setStore($store)
            ->setGroupId(2)
            ->setPrefix('Mr')
            ->setFirstname('Alen')
            ->setMiddleName('S')
            ->setLastname('Nyle')
            ->setSuffix('I')
            ->setEmail('abc2@xyz.com')
            ->setPassword('newpassword');

Result:
Magento Web Development

For the customer to be able to complete orders, we need to add address and assign it to the customer. You can refer the below code.

$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId())
        ->setFirstname($customer->getFirstname())
        ->setMiddleName($customer->getMiddlename())
        ->setLastname($customer->getLastname())
        ->setCountryId('HR')
        //->setRegionId('1') //state/province, only needed if the country is USA
        ->setPostcode('84081')
        ->setCity('Osijek')
        ->setTelephone('0038511223344')
        ->setFax('0038511223355')
        ->setCompany('brainvire')
        ->setStreet('Kersov')
        ->setIsDefaultBilling('1')
        ->setIsDefaultShipping('1')
        ->setSaveInAddressBook('1');
try{
    $address->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

Result

Magento Development Services

Please note that the setCountryId needs country code as value. You can get these values by inspecing country as an input field on customer creating page in administration. Same follows for setGroupId, you need an ID of a customer group which you’’ll find out by inspecting various elements.

I hope the above information will guide you while adding a new customer to your Magento store. Having a list of customers with all the desired information will benefit your business in great ways. Also, utilizing your pool of registered customers can help you build a strong base of returning visitors to your brand. What’s your thought on this? Leave your replies in the comment box below…!

Share Button