如何更新magento中的客户地址

时间:2013-07-26 14:47:16

标签: magento addressbook

我有这样的代码:

public function editAddress($data, $id)
{
    // $id - customer id, $data - data from form
    $customer = Mage::getModel('customer/customer')
        ->load($id);

    if (! $customer->getId() || ! $otherConditionIsValid) {
        return $this;
    }

    $dataShipping = array(
                    'firstname' => $data['firstname'],
                    'middlename' => $data['middlename'],
                    'lastname' => $data['lastname'],
                    'prefix' => $data['prefix'],
                    'suffix' => $data['suffix'],
                    'company' => $data['company'],
                    'street' => $data['street'],
                    'country_id' => $data['country'],
                    'city' => $data['city'],
                    'region_id' => '',
                    'region' => $data['region'],
                    'postcode' => $data['postal'],
                    'country_id' => $data['country'], 
                    'telephone' => $data['telephone'],
                    'fax' => $data['fax'],
    );

    $customerAddress = Mage::getModel('customer/address');

    if ($defaultShippingId = $customer->getDefaultShipping()){
        $customerAddress->load($defaultShippingId); 
    } else {   
        $customerAddress
            ->setCustomerId($post['customer_id'])
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1');   
        $customer->addAddress($customerAddress);
    }            

    try {
        $customerAddress
            ->addData($dataShipping)
            ->save();           
    } catch(Exception $e){
        Mage::log('Address Save Error::' . $e->getMessage());
    }

    return $this;
}

它不会更新数据库,但数据我正在进入magento,我非常希望你能帮助我,thx

1 个答案:

答案 0 :(得分:2)

我会对你的代码做一些小的调整。您可以在地址上尝试使用addData方法,而不是使用setData方法。我还会检查以确保您从帖子中获得有效的客户ID。我也不认为这里需要addAddress方法。

// Build billing address for customer
$dataShipping = array(
    'firstname' => $data['firstname'],
    'middlename' => $data['middlename'],
    'lastname' => $data['lastname'],
    'prefix' => $data['prefix'],
    'suffix' => $data['suffix'],
    'company' => $data['company'],
    'street' => $data['street'],
    'country_id' => $data['country'],
    'city' => $data['city'],
    'region_id' => '',
    'region' => $data['region'],
    'postcode' => $data['postal'],
    'country_id' => $data['country'], 
    'telephone' => $data['telephone'],
    'fax' => $data['fax'],
);

$customerAddress = Mage::getModel('customer/address');

if ($defaultShippingId = $customer->getDefaultShipping()){
    $customerAddress->load($defaultShippingId); 
} else {   
    $customerAddress
        ->setCustomerId($post['customer_id'])
        ->setIsDefaultBilling('0')
        ->setIsDefaultShipping('1')
        ->setSaveInAddressBook('1');
}

try {
    $customerAddress->setData($dataShipping);
    $customerAddress->save();
} catch (Exception $ex) {
    Mage::log('Address Save Error::' . $e->getMessage());
}