0

I am trying to update a customer's default billing address when they update their main account information.

My observer function looks like this and for the time being I am hard coding in the prefix update to be Dr:

public function customerSave($observer)
{
        $customer           = $observer->getCustomer();
        $customerAddress    = Mage::getModel('customer/address');

        if ($customer->getDefaultBilling()){
             $defaultBillingId = $customer->getDefaultBilling();
             $customerAddress->load($defaultBillingId);
        }
        $customerAddress->setPrefix('Dr')->save();

        Mage::app()->getResponse()->setRedirect(Mage::getUrl('checkout/onepage'))->sendResponse();
        exit;
}

UPDATE: If I remove the redirect it works. So why does this affect it as the observer is customer_save_after which means complete action after the data has been saved

Could someone point out where I am going wrong

0

In the end I had to override the editPostAction in the account controller and complete the redirect in there

0

Sorry its so damned late Goose!

The problem is that in the Magento customer_save_after event, the customer and addresses are saved (of course) and assigned ID's, however the customer in the data object or observer is still the customer model that was saved.

while its details are correct, when a customer modle is loaded, and specifically when the address collection is loaded, the id's are mapped into a private getbyids array. because when the addresses where added to the customer (before the save) they had no id's (not until after the save) this map is empty.

You can fix this by reloading the customer Model, as the addresses are also reloaded, becasue they are loaded (not added then saved) the id map gets correctly populated.

$customer = Mage::getModel('customer/customer')->load($observer->getCustomer()->getEntityId());

There is however some overhead in reloading the whole customer model, I have found the following snippet works to reload just the address collection, thus reducing the reloading overhead.

this is just an alternative answer for future searchers:

//assuming $customer is our customer model

//clear address collection
//note do not daisy chain this method as it does not return the customer object!
$customer->cleanAllAddresses();

//getting default addresses now works!
$myDefaultBill = $address->getDefaultBillingAddress();

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.