<?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(); ?>
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(); ?>
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.
dfasd fdf
ReplyDeleteThats nice post about magento ecommerce. magento ecommerce site is one of the best shopping site.magento ecommerce platform
ReplyDelete