1

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?

3 Answers 3

1

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.

1

I think you are setting an invalid customer id on customer address entity and hence you are getting this error. Please try below code, which is an alternative solution in this context.

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

//make sure customer exist
if (isset($customer->getId())) {
    $customerAddress = $customer->getAddressById($data['address_id']);

    //make sure address is really associated with the customer who is editing
    if (isset($customerAddress->getId())) {
        $customerAddress->setWebsiteId(1);
        $customerAddress->setStore(1);      
        $customerAddress->setFirstname('test');
        $customerAddress->save();
    } else {
        Mage::throwException('Address is not associated  with this customer !');
    }

} else {
    Mage::throwException('Customer is not valid customer !');
}

You can also go on with @Adarsh's Answer as it really checks customer exist or not.

0

Please check below code:-

    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
    $customerAddress = Mage::getModel('customer/address');
    $customerAddress->load($customerAddressId);
    $customerAddress->setCustomerId($customer->getId());
    $customerAddress->setWebsiteId(1);
    $customerAddress->setStore(1);      
    $customerAddress->setFirstname('test');
    $customerAddress->save();

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.