gpt4 book ai didi

magento - 从客户地址中删除地址

转载 作者:行者123 更新时间:2023-12-02 18:37:44 25 4
gpt4 key购买 nike

我想删除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();
}
}

}
?>

那么如何防止删除默认帐单和送货地址。请帮忙

最佳答案

在地址收集中应该有一个函数来获取地址是否是默认的计费或送货地址:

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)
}
}

关于magento - 从客户地址中删除地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com