2

I am trying to fetch customer addresses on a custom page using below code.

$shipId = 'VALID SHIPPING ADDRESS ID';
$billId = 'VALID BILLING ADDRESS ID';
$addressObject = Mage::getModel('customer/address');
$shipAddress = $addressObject->load($shipId);
$billAddress = $addressObject->load($billId);

But I don't know why I am getting only the billing address data in $shipAddress and $billAddress.
Actually I am getting the address which is lower in the order. For example if modify the code like below

$billAddress = $addressObject->load($billId);
$shipAddress = $addressObject->load($shipId);

Now I am getting the shipping address in both the variables.
Please let me know what is the issue here and how this can be fixed.

1

You have a misconception in how Magento models work.

They are not "loaders" which just give you back a data object.

They represent the entity itself and load themselves (this is called "Active Record").

So you have a model $addressObject and load it twice, from different ids. The second load overwrites the data from the first load.

Your code only works because load() happens to return $this for convenience. In the end $billAddress, $shipAddress and $addressObject all hold the identical object. Objects in PHP are not copied when you assign them to a different variable, the variable only holds a reference to the actual object.

Correct code:

$billId = 'VALID BILLING ADDRESS ID';
$shipAddress = Mage::getModel('customer/address')->load($shipId);
$billAddress = Mage::getModel('customer/address')->load($billId);
  • I have already the code you have mentioned, but it is also not working. – Anshu Mishra Aug 24 '16 at 6:52
  • Sorry for the previous comment, you are right and the code is working. I am using the same code. – Anshu Mishra Aug 24 '16 at 6:59

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.