1

我有这样的代码:

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中获取的数据数组,我非常希望你能帮助我,谢谢

4

1 回答 1

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());
}
于 2013-07-26T16:14:48.020 回答