Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento create customer with address programmatically

If you want to create new customer with address programmatically you can easily create with this script. You can also create customer out side magento using this script.

First you need to add Mage.php file if you are adding customer from out side magento.

require_once ("app/Mage.php");
umask(0);

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('Hardik')
            ->setLastname('Patel')
            ->setEmail('hkpatel201@gmail.com')
            ->setPassword('somepassword');
 
try{
    $customer->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

For the customer to be a complete entity able to complete orders, we need to add an address and assign it to a customer. Let’s do just that.

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

Download Source

Please support us, Like us on Facebook.

0 comments:

Post a Comment

 

Copyright @ 2017 HKBlog.