-
-
Save davirs/5d820150257e4ba088b03e20db8b25a7 to your computer and use it in GitHub Desktop.
Remove all orders Magento 1.9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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