用于删除magento中的客户,地址和相关订单的代码

时间:2014-11-25 20:32:41

标签: php magento

在magento中,我们是否可以选择使用php代码删除客户以及他的地址和订单?如果是的话,我想知道php代码。我知道,从管理员我们可以选择删除客户。但我们需要PHP代码或查询来手动执行此过程。提前致谢。

1 个答案:

答案 0 :(得分:1)

使用以下代码在magento根文件夹上创建一个standalnoe脚本:

<?php
include_once('app/Mage.php');
Mage::app();

$customerId = 10;  // Here you can specify customer ID

Mage::register('isSecureArea', TRUE);

$orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_id', $customerId);

$orderIds = array();        
foreach($orders as $order) {
    $orderIds[] = $order->getId();      
}

foreach($orderIds as $id){
    try{
        Mage::getModel('sales/order')->load($id)->delete();
        echo "order #".$id." is Deleted <br>";
    }catch(Exception $e){
        echo "order #".$id." could not be Deleted: ".$e->getMessage() ."<br>";
    }
}

try{
    Mage::getModel("customer/customer")->load($customerId)->delete();
    echo "Customer #".$customerId." Deleted Successfully <br>";
}catch(Exception $e){
    echo "Customer #".$customerId." could not be Deleted: ".$e->getMessage() ."<br>";
}



Mage::unregister('isSecureArea');

die('Deletion DONE');

希望这有帮助!!