I am trying to create or update default shipping address for a customer using customer_save_after event (based on some custom registration fields) when customer tries to create new account or edit his account.
Here goes the portion of the Observer model code:
...
$customer = $observer->getEvent()->getCustomer();
if ($customer->getId() && $otherConditionIsValid){
$dataShipping = array(
'firstname' => $someFixedFirstName,
'lastname' => $someFixedLastName,
'street' => array($someFixedStreetLine),
'city' => $someFixedCity,
'region' => $someFixedState,
'region_id' => '',
'postcode' => $someFixedZipcode,
'country_id' => $someFixedCountry,
'telephone' => $someFixedTelephone,
);
$customerAddress = Mage::getModel('customer/address');
if($defaultShippingId = $customer->getDefaultShipping()){ //if customer already has default shipping address
$customerAddress->load($defaultShippingId);
}
$customerAddress->setData($dataShipping)
->setCustomerId($customer->getId())
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
$customer->addAddress($customerAddress); #setting Shipping Address to be added/updated
//When i try to use below commented code, script gets called for infinite times, finally running out of memory
/*try{
$customerAddress->save();
}catch(Exception $e){
Mage::log('Address Save Error::' . $e->getMessage());
}*/
}
...
Above code works fine when customer creates a new account. However default shipping address don’t get updated when customer tries to edit the custom registration field from My Account > Account Information
So my main concern is how to update the shipping address using $customer object or any other code using customer_save_after event.
If I understand correctly, you would like to do the following when the
customer_save_afterevent gets fired:If the customer does not have a default shipping address, then create one with the values in the $dataShipping array.
If the customer does have a default shipping address, then update it’s values with those in the $dataShipping array.
If this is indeed what you are looking for then please try the code below (obviously you need to take care of where the values for the $otherConditionIsValid and $dataShipping variables):