Magento – How to edit an address from the address book of the customer using the address id

addresscustomercustomer-addressmagento-1.8

I have to edit the address of the customer programmatically using the address id. Here I have customer id and address id of the address to be edited. I tried the following code. But it is not working.

$customerAddress = Mage::getModel('customer/address');
$customerAddress->load($data['address_id']);
$customerAddress->setCustomerId($data['customer_id']);
$customerAddress->setWebsiteId(1);
$customerAddress->setStore(1);      
$customerAddress->setFirstname('test');
$customerAddress->save();

It gives following error Call to a member function getStore() on boolean in ../app/code/core/Mage/Customer/Model/Observer.php

I need to edit all fields of the address. Can anyone help on this?

Best Answer

Try this:

$customerAddress = Mage::getModel('customer/address');
$customerAddress->load($data['address_id']);

$customer = Mage::getModel('customer/customer')->load($data['customer_id']);
if($customer->getId()){ //if customer exist
  $customerAddress->setCustomer($customer);
  $customerAddress->setWebsiteId(1);
  $customerAddress->setStore(1);      
  $customerAddress->setFirstname('test');
  $customerAddress->save();
}

Hope this helps.

Related Topic