January 2, 2010

Programmaticaly adding new customers to the Magento store

Every now and then you will have specific case in Magento store where you might need to programmaticaly add new customers. Adding customer with basic information like First name, Last name, email and password is relatively trivial task. Here is the sample (working code):
<?php
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
 
//$customer = new Mage_Customer_Model_Customer();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId; 
$customer->setStore($store);
 
$customer->firstname = "Branko";
$customer->lastname = "Ajzele";
$customer->email = "ajzele@someserver.com";
$customer->password_hash = md5("mycoolpass");
$customer->save();
?>
Here are some screenshots of the final result:
customer1
customer2
customer3
Ok, so now we now how to add new customer from our code. But this is bare minimum. Where are the addresses you say? Well, below is the code that fulfills the above one and ads an address information to the customer.
<?php 
 
$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();
 
//$customer = new Mage_Customer_Model_Customer();
$customer = Mage::getModel("customer/customer");
$customer->website_id = $websiteId; 
$customer->setStore($store);
 
$customer->firstname = "Branko";
$customer->lastname = "Ajzele";
$customer->email = "ajzele@someserver.com";
$customer->password_hash = md5("mycoolpass");
$customer->save();
 
//$address = new Mage_Customer_Model_Address();
$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId());
$address->firstname = $customer->firstname;
$address->lastname = $customer->lastname;
$address->country_id = "HR"; //Country code here
$address->postcode = "31000";
$address->city = "Osijek";
/* NOTE: If country is USA, please set up $address->region also */
$address->telephone = "0038531444888";
$address->fax = "0038531555999";
$address->company = "ActiveCodeline";
$address->street = "My Cool Street";
 
$address->save();
 
?>
And below is the screenshot that reflex the state after executing code above.
customer6
As you can see, adding a new customer from within a code is pretty straightforward process. The thing you should keep an eye on are the required fields. Useful way achieving a proper result is to try to add the customer from within Magento admin interface with all the required address fields then do a little “reverse engineering” by loading and dumping the loaded instance of Mage_Customer_Model_Customer and Mage_Customer_Model_Address objects. Examining the dumped ($object->debug()) structure would give you a pretty good idea of what needs to be provided to object when it is created programmaticaly.
Hope I don’t have to mention that all of this “playing” should first be done on developer machine, never on live site.
Hope this was helpful. Cheers.

2 comments:

  1. Thats nice post about magento ecommerce. magento ecommerce site is one of the best shopping site.magento ecommerce platform

    ReplyDelete