1

我想删除 Magento 中与客户关联的所有地址(帐单和送货地址)。我怎样才能以编程方式做到这一点?有人可以帮帮我吗?

我正在使用这段代码:

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

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

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

    $address->delete();
4

2 回答 2

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();
            }
        }
    }
于 2013-03-02T15:49:55.153 回答
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();

}

删除所有地址

这是一个坏主意,因为地址上有很多外键。不要那样做。

于 2013-03-02T11:41:47.107 回答