【问题标题】:Magento - How to update shipping address using customer_save_after event?Magento - 如何使用 customer_save_after 事件更新送货地址?
【发布时间】: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 事件的任何其他代码更新送货地址。

【问题讨论】:

    标签: php magento


    【解决方案1】:

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

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

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

    如果这确实是您正在寻找的,那么请尝试下面的代码(显然您需要注意 $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; 
    

    【讨论】:

    • 非常感谢德鲁!这就像一个魅力。我认为送货地址的条件设置起到了作用。干杯!
    • 对我不起作用 - 我得到 Call to a member function getDefaultShipping() on a non-object,因为似乎 $customer = $observer->getEvent()->getCustomer() 返回的是客户 ID 而不是客户对象
    【解决方案2】:
    $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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 2014-08-30
      • 1970-01-01
      相关资源
      最近更新 更多