2014-10-01 99 views
2

我想刪除在magento後端沒有默認的客戶地址。我已經使用下面的代碼來刪除,但它從通訊錄中刪除所有地址。從客戶地址刪除地址

<?php 
$customer = Mage::getModel('customer/customer')->load(2); 
if($customer){ 

     /*Load the customer addresses by Customer Id*/ 
     $customerAddressCollection = Mage::getResourceModel('customer/address_collection')->addAttributeToFilter('parent_id',$customer->getId())->getItems(); 
     echo '<pre>'; 
     foreach($customerAddressCollection as $customerAddress){ 
      var_dump($customerAddress->getData()); 
      $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(); 
      } 
     } 

    } 
?> 

那麼如何防止刪除默認的賬單和送貨地址。 請幫助

回答

3

在地址收集應該有函數來得到,如果地址是默認賬單或郵寄:

if (! $customer->getDefaultBillingAddress()) { 
$def_billing = 0; 
} else { 
$def_billing = $customer->getDefaultBillingAddress(); 
$def_billing = $def_billing->getData('entity_id'); //??? test it, or $def_billing->getId() 
} 
// in this time change $def_billing with ID of adress and edit my answer please 

if (! $customerObj->getDefaultShippingAddress()) { 
$def_shipping = 0; 
} else { 
$def_shipping = $customer->getDefaultShippingAddress(); 
$def_shipping = $def_shipping ->getData('entity_id'); //??? test it, or $def_shipping ->getId() 
} 

// in this time change $def_shipping with ID of adress and edit my answer please 


//If you find the function working, you can use condition: 

    foreach($customerAddressCollection as $customerAddress){ 
    if($def_shipping == $customerAddress->getData('entity_id') || $def_billing == customerAddress->getData('entity_id')) { 
     // skip 
    } else { 
     // do the deletng job ($customerAddress object is not shipping or billing default) 
    } 
    } 
+0

抱歉,但它不工作 – Zaheerabbas 2014-10-01 10:05:39

+0

編輯,請使用$ customer-> getDefaultBillingAddress(),並且您必須獲得此地址集的ID $ def_billing = founded_ID; $ def_shipping = created_ID並將其添加到我的答案中,或者發佈到com我會爲其他thx芬蘭人的答案 – Martin 2014-10-01 10:15:14

3

最終的答案與客戶收集

<?php 
$collection = Mage::getModel('customer/customer')->getCollection(); 
foreach($collection as $customer){ 
$customer = Mage::getModel('customer/customer')->load($customer->getId()); 
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->getDefaultBillingAddress()) { 
      } else { 
      $def_billing = $customer->getDefaultBillingAddress(); 
      $def_billing = $def_billing->getData('entity_id'); //??? test it, or $def_billing->getId() 
      } 
      // in this time change $def_billing with ID of adress and edit my answer please 

      if (! $customer->getDefaultShippingAddress()) { 
      } else { 
      $def_shipping = $customer->getDefaultShippingAddress(); 
      $def_shipping = $def_shipping ->getData('entity_id'); //??? test it, or $def_shipping ->getId() 
      } 
      if($def_shipping == $customerAddress->getData('entity_id') || $def_billing == $customerAddress->getData('entity_id')){ 
     /*Load the Customer Address by ID and delete it*/  
       $customer_address_id = $customerAddress->getData('entity_id'); 

      } else { 
     // do the deletng job ($customerAddress object is not shipping or billing default) 


       Mage::getModel('customer/address')->load($customer_address_id)->delete(); 
      } 
     } 

    } 
}exit; 
?>