Thursday, April 26, 2012

Adding address programmatically in magento

$addressData =  array (
                    [prefix] => test
                    [firstname] => test
                    [middlename] => test
                    [lastname] => test
                    [suffix] => tes
                    [company] => test
                    [street] => 34
24
                    [city] => 234
                    [country_id] => US
                    [region] => Alabama
                    [region_id] => 1
                    [postcode] => 234
                    [telephone] => 234
                    [fax] => 234
                    [is_default_billing] => 1
                    [is_default_shipping] => 
                );
 
 $customer = Mage::getModel('customer/customer');
 $customer->loadByEmail('test@test.com');
 $address   = Mage::getModel('customer/address');
 $address->addData($addressData);
 $customer->addAddress($address);
 
 $customer->save();
 

3 comments:

  1. $addressData = array (
    'prefix' => 'test',
    'firstname' => 'test',
    'middlename' => 'test',
    'lastname' => 'test',
    'suffix' => 'test',
    'company' => 'test',
    'street' => 34,
    'city' => 234,
    'country_id' => 'US',
    'region' => 'Alabama',
    'region_id' => 1,
    'postcode' => 234,
    'telephone' => 234,
    'fax' => 234,
    'is_default_billing' => 1,
    'is_default_shipping' => 1,
    );
    Fixed and/or updated the format for you. Strings in php need to be wrapped in either " or '. Arrays items are separated by commas. I'm not sure what is going on with the [ ] but it breaks in CE 1.9.0.1. I am going to assume that all of the more recent version of magento use the format above.

    ReplyDelete
  2. This post is really help full for me. Thanks to both of you.

    ReplyDelete