2013-07-26 26 views
1

更新客戶地址,我有一個這樣的代碼:如何在Magento

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

回答

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()); 
} 
+0

我有這樣的錯誤日誌中:地址保存錯誤:: SQLSTATE [23000]:完整性約束違規:1452不能添加或更新子行,外鍵約束失敗('shopping_app'.'customer_address_entity', CONSTRAINT'FK_CUSTOMER_ADDRESS_ENTITY_PARENT_ID_CUSTOMER_ENTITY_ENTITY_ID' FOREIGN KEY('parent_id')REFERENCES'customer_entity'('entity_id')ON DE)。您能否爲我解釋一下,thx – user2519904

+0

您確定您收到的客戶ID是否有效?我也注意到了這一點,你得到的默認運輸是這樣的:$ customer-> getDefaultShipping(),但要獲得ID,你需要得到它像這樣$ customer-> getDefaultShipping() - > getId(),否則你會得到整個地址對象,而不僅僅是ID,我想你打算在這裏使用ID。 –