How to programmatically create customer in Magento?

Magento is one of the most flexible and widely used e-commerce platforms nowadays. Many times during our development we need to create customers via our code. For example, if we are developing a Magento plugin to connect our Magento store to marketplaces like Walmart, Etsy etc then we create an order for the customer via our code. But to create an order we need to create customer if that customer does not exist on our Magento store. We can create customer in Magento simply by using the following code.

Code for creating a customer in Magento:

$_website = Mage::app()->getWebsite()-->getId();
        $store = Mage::app()->getStore();       
        $_firstName = 'Jhon';
        $_lastName = 'Doe';
        $_customerEmail = 'demo@demo.com';
        $_customerPassword = '#KnowbandPlugins';
        
        $_newCustomer = Mage::getModel("customer/customer");
        $_newCustomer->setWebsiteId($_website)
                     ->setStore($store)
                     ->setFirstname($_firstName)
                     ->setLastname($_lastName)
                     ->setEmail($_customerEmail)
                     ->setPassword($_customerPassword);
        try{
            $_newCustomer->save();
        }
        catch (Exception $e) {
            Mage::getSingleton('customer/session')->addException($e, $this->__('Error in creating customer'));
            Mage::logException($e);
        }
<a href="https://www.knowband.com/blog/wp-content/uploads/2018/10/untitled-26.png"><img class="alignnone size-medium wp-image-22306" src="https://www.knowband.com/blog/wp-content/uploads/2018/10/untitled-26-300x48.png" alt="creating-customer-in-magento" width="300" height="48" /></a>

But this customer only has very basic information like first name, last name, email etc. It does not have shipping or billing address. We can save complete address information of the customer via the following code.

Code for saving the address of the customer:

$_website = Mage::app();getWebsite();
$_customerEmail = 'demo@demo.com'; //Customer Email
$_countryId = 'IN';
        $_postCode = '201301';
        $_city = 'Noida';
        $_telephone = '1111111111';
        $_fax= '';
        $_company = 'Velocity Software Solutions';
        $_street = '';
        
        $_customer = Mage::getModel('customer/customer')
                ->setWebsiteId($_website->;getId())
                ->loadByEmail($_customerEmail);
        $_customerAddress = Mage::getModel("customer/address");
        $_customerAddress->setCustomerId($_customer->getId())
                ->setFirstname($_customer->getFirstname())
                ->setMiddleName($_customer->getMiddlename())
                ->setLastname($_customer->getLastname())
                ->setCountryId($_countryId)
                ->setPostcode($_postCode)
                ->setCity($_city)
                ->setTelephone($_telephone)
                ->setFax($_fax)
                ->setCompany($_company)
                ->setStreet($_street)
                ->setIsDefaultBilling('1')
                ->setIsDefaultShipping('1')
                ->setSaveInAddressBook('1');

        try{
            $_customerAddress->save();
        }
        catch (Exception $e) {
            Mage::getSingleton('customer/session')->addException($e, $this->__('Error in saving customer address.'));
            Mage::logException($e);
        }
<a href="https://www.knowband.com/blog/wp-content/uploads/2018/10/untitled-27.png"><img class="alignnone size-medium wp-image-22308" src="https://www.knowband.com/blog/wp-content/uploads/2018/10/untitled-27-300x133.png" alt="saving-address-of-customer" width="300" height="133" /></a>

Now the customer address information is complete. Hope the above-mentioned code will help you in adding a new customer to your Magento store.

Prashant Kumar

Prashant Kumar

Prashant is a passionate Magento developer. He loves exploring e-commerce and reading books.

Leave a Reply

Your email address will not be published. Required fields are marked *