Skip to content

Instantly share code, notes, and snippets.

@luizwbr
Last active November 28, 2019 16:24
Show Gist options
  • Save luizwbr/1cb4cfc83ae07db4f085ba245c54b16e to your computer and use it in GitHub Desktop.
Save luizwbr/1cb4cfc83ae07db4f085ba245c54b16e to your computer and use it in GitHub Desktop.
Remove all orders Magento 1.9
<?php
require_once('app/Mage.php');
ini_set('display_errors', 1);
Mage::app('admin');
$orderIds = array(1,2,3,4);
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('entity_id', (array) $orderIds);
foreach ($orders as $o) {
//load order object - I know it's not ok to use load in a loop but it should be ok since it's a one time script
$order = Mage::getModel('sales/order')->load($o->getId());
$invoices = $order->getInvoiceCollection();
foreach ($invoices as $invoice){
//delete all invoice items
$items = $invoice->getAllItems();
foreach ($items as $item) {
$item->delete();
}
//delete invoice
$invoice->delete();
}
$creditnotes = $order->getCreditmemosCollection();
foreach ($creditnotes as $creditnote){
//delete all creditnote items
$items = $creditnote->getAllItems();
foreach ($items as $item) {
$item->delete();
}
//delete credit note
$creditnote->delete();
}
$shipments = $order->getShipmentsCollection();
foreach ($shipments as $shipment){
//delete all shipment items
$items = $shipment->getAllItems();
foreach ($items as $item) {
$item->delete();
}
//delete shipment
$shipment->delete();
}
//delete all order items
$items = $order->getAllItems();
foreach ($items as $item) {
$item->delete();
}
//delete payment - not sure about this one
$order->getPayment()->delete();
//delete quote - this can be skipped
if ($order->getQuote()) {
foreach ($order->getQuote()->getAllItems() as $item) {
$item->delete();
}
$order->getQuote()->delete();
}
//delete order
$order->delete();
}
// erases order grid data
$connection = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "DELETE FROM `mg19_sales_flat_order_grid` WHERE entity_id IN ('".implode(",",$orderIds)."');";
echo $sql;
$rows = $connection->fetchAll($sql);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment