Magento - 如何使用customer_save_after事件更新送货地址?

时间:2012-06-23 20:19:54

标签: php magento

当我尝试创建新帐户或修改帐户时,我正尝试使用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事件的任何其他代码更新送货地址。

2 个答案:

答案 0 :(得分:5)

如果我理解正确,您希望在customer_save_after事件被触发时执行以下操作:

  1. 如果客户没有默认送货地址,请使用$ dataShipping数组中的值创建一个。

  2. 如果客户确实有默认送货地址,请使用$ dataShipping数组中的值更新其值。

  3. 如果确实如此,请尝试下面的代码(显然你需要处理$ otherConditionIsValid和$ dataShipping变量的值):

    $customer = $observer->getEvent()->getCustomer();
    if (! $customer->getId() || ! $otherConditionIsValid){
        return $this;                
    }
    
    $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()){
         $customerAddress->load($defaultShippingId); 
    } else {   
         $customerAddress
            ->setCustomerId($customer->getId())
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1')
         ;   
    
         $customer->addAddress($customerAddress);
    }            
    
    try {
        $customerAddress
            ->addData($dataShipping)
            ->save()
        ;           
    } catch(Exception $e){
        Mage::log('Address Save Error::' . $e->getMessage());
    }
    
    return $this; 
    

答案 1 :(得分:0)

$address = Mage::getModel("customer/address");              
   $address->setCustomerId($this->customerID());
   $address->setFirstname(Mage::app()->getRequest()->getPost("event_firstname"));
   $address->setLastname(Mage::app()->getRequest()->getPost("event_lastname"));
   $address->setCountryId('AE');
   $address->setCity(Mage::app()->getRequest()->getPost("event_city"));
   $address->setStreet(Mage::app()->getRequest()->getPost("event_address"));
   $address->setTelephone(Mage::app()->getRequest()->getPost("event_mobile"));
   $address->save();