2013-03-02 50 views
1

我想刪除Magento中所有與客戶相關的地址(賬單和送貨地址)。我如何以編程方式執行此操作?有人可以幫我嗎?清理Magento中的客戶賬單和送貨地址

我使用這個代碼:

$customer = Mage::getModel('customer/customer'); 
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); 

    $customer->loadByEmail((string) $_REQUEST['email']); 

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

    $address->delete(); 

回答

0

你想刪除的關係,或刪除所有的地址?

關係

$col = Mage::getModel('customer/customer')->getCollection() 
foreach($col as $customer) { 
    $address = $customer->getDefaultBillingAddress(); 
    // set attributes 
    $address->save(); 

    $address = $customer->getDefaultShippingAddress(); 
    // set attributes 
    $address->save(); 

} 

刪除所有地址

這是一個壞主意,因爲有上的地址大量的外鍵。不要這樣做。

+0

我想刪除地址並想要設置新的運輸和帳單地址。 – tinkesh 2013-03-02 11:47:19

+0

爲什麼要刪除它們?加載並更改它們。 – 2013-03-02 12:06:59

+0

這是我的動機之一。我想做這個。你可以幫我嗎? – tinkesh 2013-03-02 12:19:51

3

假設您已從客戶收集中加載客戶。找到下面的代碼,使用客戶ID加載客戶地址並逐個刪除。

if($customer){ 
     /*Load the customer addresses by Customer Id*/ 
     $customerAddressCollection = Mage::getResourceModel('customer/address_collection')->addAttributeToFilter('parent_id',$customer->getId())->getItems(); 
     foreach($customerAddressCollection as $customerAddress){ 
      $customer_address_id = $customerAddress->getData('entity_id'); 
      if($customer_address_id!=""){ 
     /*Load the Customer Address by ID and delete it*/  
       Mage::getModel('customer/address')->load($customer_address_id)->delete(); 
      } 
     } 
    } 
+0

優秀的工作..救了我的時間:-) +1 – Butterfly 2015-08-03 10:47:26

相關問題