当客户尝试创建新帐户或编辑他的帐户时,我正在尝试使用customer_save_after
事件(基于一些自定义注册字段)为客户创建或更新默认送货地址。
下面是 Observer 模型代码的一部分:
...
$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());
}*/
}
...
当客户创建新帐户时,上面的代码可以正常工作。但是,当客户尝试从以下位置编辑自定义注册字段时,默认送货地址不会更新My Account > Account Information
所以我主要关心的是如何使用$customer
对象或任何其他使用customer_save_after
事件的代码来更新送货地址。