Skip to content

Instantly share code, notes, and snippets.

@mehdichaouch
Created November 21, 2017 09:19
Show Gist options
  • Save mehdichaouch/ef267bdef2e2af4e7d981e23bf0038ee to your computer and use it in GitHub Desktop.
Save mehdichaouch/ef267bdef2e2af4e7d981e23bf0038ee to your computer and use it in GitHub Desktop.
Magento - Delete order (place this in index.php and add id(s))
<?php
// src : https://magento.stackexchange.com/a/21744/56191
// Thanks to Marius ;)
$orderIds = array (...);
$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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment