我想删除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 :(得分: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();
}
}
}
答案 1 :(得分: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();
}
这是一个坏主意,因为地址上有很多外键。不要那样做。