Magento获取包含已删除产品的订单列表

时间:2016-05-24 15:16:21

标签: php magento-1.9 orders

我想从Magento商店数据库中删除陈旧数据 它目前包含从系统中删除产品的订单 我如何检索此类订单的数据?如果需要,我如何能够删除这些订单?

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码删除甚至单个产品/项目不再存在的订单: -

<?php

ini_set('display_errors', 1);
require_once 'app/Mage.php';
Mage::app();
Mage::register('isSecureArea', true);

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


foreach ($orders as $order) {
    $item_ids = $order->getAllItems();

    foreach ($item_ids as $item) {
        $product = Mage::getModel('catalog/product')->load($item->getProductId());

        $product_id = $product->getData('entity_id');

        if (!$product_id) {
            $order->delete();
            remove($order->getId());
        }
    }
}

function remove($order_id) {
    $resource = Mage::getSingleton('core/resource');
    $delete = $resource->getConnection('core_read');
    $order_table = $resource->getTableName('sales_flat_order_grid');
    $invoice_table = $resource->getTableName('sales_flat_invoice_grid');
    $shipment_table = $resource->getTableName('sales_flat_shipment_grid');
    $creditmemo_table = $resource->getTableName('sales_flat_creditmemo_grid');
    $sql = "DELETE FROM  " . $order_table . " WHERE entity_id = " . $order_id . ";";
    $delete->query($sql);
    $sql = "DELETE FROM  " . $invoice_table . " WHERE order_id = " . $order_id . ";";
    $delete->query($sql);
    $sql = "DELETE FROM  " . $shipment_table . " WHERE order_id = " . $order_id . ";";
    $delete->query($sql);
    $sql = "DELETE FROM  " . $creditmemo_table . " WHERE order_id = " . $order_id . ";";
    $delete->query($sql);

    return true;
}