【发布时间】:2012-06-25 17:24:51
【问题描述】:
当客户尝试创建新帐户或编辑其帐户时,我正在尝试使用 customer_save_after 事件(基于一些自定义注册字段)为客户创建或更新默认送货地址。
观察者模型代码部分如下:
...
$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 事件的任何其他代码更新送货地址。
【问题讨论】: