0

I am facing a strange behavior in the following code (Magento EE 1.14.2.0). Though it's an update code as it's being loaded by ID, it's always creating a new entries.

$customerId = 123;
$addressId  = 123;

echo '<hr />';
echo 'Loading address::' . $addressId . '<br />';
$address = Mage::getModel('customer/address');
$address->load($addressId);
$addressData = array (
    'country_id' => 'US',
    'street' =>
        array (
            0 => 'street 1',
            1 => 'street 2',
        ),
    'telephone' => '+1-000000',
    'postcode'  => 'xxx',
    'city'      => 'NY',
    'region'    => 'NY',
    'firstname' => 'John',
    'lastname'  => 'Doe',
);

$address->setData($addressData);
$address->setCustomerId($customerId);
$address->save();
echo '<hr />';
Zend_Debug::dump(
    $address->getData()
);

I checked Customer Address Api & My Account Address section, they are also using the similar code.

Do you guys see any issue with the above code?

1 Answer 1

1

Use $address->addData($addressData) instead of $address->setData($addressData) since setData will overwrite all the data in the object, including entity_id.

2
  • My bad :) I was too fast to post the Question. Either setData($key, $value) or addData($array) fixes the issue.
    – MagePsycho
    Commented Feb 24, 2016 at 10:22
  • 1
    @MagePsycho OR use $address->setData($addressData)->setId($addressId)->save(); Commented Feb 24, 2016 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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