0

I create customer programatically, bellow is my code

$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->setStore(Mage::app()->getStore());
$customer->setFirstname($fname);
$customer->setLastname($lname);
$customer->setEmail($email);
$customer->setPassword($password);
$customer->save(); 

$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId());
$address->setFirstname($customer->getFirstname());
$address->setLastname($customer->getLastname());
$address->setCountryId($addressData['country']); //Country code here
$address->setRegionId($addressData['RegionId']);
$address->setCity($addressData['City']);
$address->setPostcode($addressData['Zipcode']);
$address->setTelephone($addressData['Telephone']);
$address->setFax($addressData['Fax']);
$address->setCompany($addressData['Company']);
$address->setStreet($addressData['Street']);
$address->setIsDefaultBilling('1');
$address->setIsDefaultShipping('1');
$address->setSaveInAddressBook('1'); 
try
{
    $address->save(); 
}
catch (Exception $e) 
{
    $msg = Zend_Debug::dump($e->getMessage());
}

It is working good but i want add different shipping address, and not set it as Default billing address, means my shipping address is difference.

How can i save shipping address in this ?

0

Try this code to use difference shipping address:

 <?php 
    $customer = Mage::getModel("customer/customer");
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
    $customer->setStore(Mage::app()->getStore());
    $customer->setFirstname($fname);
    $customer->setLastname($lname);
    $customer->setEmail($email);
    $customer->setPassword($password);
    $customer->save(); 
    $address = Mage::getModel("customer/address");
    $address->setCustomerId($customer->getId());
    $address->setFirstname($customer->getFirstname());
    $address->setLastname($customer->getLastname());
    $address->setCountryId($addressData['country']); //Country code here
    $address->setRegionId($addressData['RegionId']);
    $address->setCity($addressData['City']);
    $address->setPostcode($addressData['Zipcode']);
    $address->setTelephone($addressData['Telephone']);
    $address->setFax($addressData['Fax']);
    $address->setCompany($addressData['Company']);
    $address->setStreet($addressData['Street']);
    $address->setIsDefaultBilling('1');
    $address->setIsDefaultShipping('1');
    $address->setSaveInAddressBook('1'); 
    /* add shipping address */
    $shipAddress = array (
        'firstname' => 'Arun',
        'lastname' => 'Rai',
        'street' => array (
            '0' => 'Sample address part1',
            '1' => 'Sample address part2',
        ),
        'city' => 'Chandigarh',
        'region_id' => '',
        'region' => '',
        'postcode' => '160017',
        'country_id' => 'IN', /* Croatia */
        'telephone' => '7814075655',
    );
    $customAddress = Mage::getModel('customer/address');
    $customAddress->setData($shipAddress)
                ->setCustomerId($customer->getId())
                ->setIsDefaultShipping('1')
                ->setSaveInAddressBook('1');

    try
    {
        $address->save(); 
        /* save shipping address */
        $customAddress->save();
    }
    catch (Exception $e) 
    {
        $msg = Zend_Debug::dump($e->getMessage());
    }
    ?>
  • thanks @Arunendra, can you please help to add State/Province because i add State/Province as NY or GA or NC and not have State/Province code – Rohit May 13 '16 at 9:11
  • You need to put State/Province id here like = 1 or 2 etc. review this table he directory_country_region you may accept this answer (upvote ) if it's solve your problem thanks :) – Arunendra May 13 '16 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.