0

I want to delete order programmatically. So I want to delete sales order from sales->orders grid as well as sales->invoices grid, sales->shipments grid. For that, I got a link here These site's code is not working on Invoice Delete and on Shipment Delete in Magento1.9.0.1 programmatically. it only deletes the order from sales->order list, not invoice from sales->invoices and not delete shipment from sales->shipments. I search a lot in google, but there is no suitable result "How to delete invoice and shipment and creditmemo programmatically in Magento 1.9.0.1".If any one knows about this issue, please reply it.

5

I have got a success after long process/RND and searching on google a lot.

   $orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('entity_id', (array) $orderId);
                    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();

                        $order_increment_id = $order->getIncrementId();
                        /*if($order_increment_id){
                           $invoice_in_grid = Mage::getResourceModel('sales/invoice_grid_collection')->addFieldToFilter('increment_id',$order_increment_id);
                           $invoice_in_grid->delete(); 

                           $shipment_in_grid = Mage::getResourceModel('sales/shipment_grid_collection')->addFieldToFilter('increment_id',$order_increment_id);
                           $shipment_in_grid->delete(); 
                       }*/

                       $db = Mage::getSingleton('core/resource')->getConnection('core_write');
                        $sales_flat_order_grid = Mage::getSingleton('core/resource')->getTableName('sales_flat_order_grid');  
                        $sales_flat_invoice_grid = Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice_grid');
                        $sales_flat_shipment_grid = Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_grid');
                        $order_increment_id = $o->getIncrementId();

                        if($order_increment_id)
                        { 
                        /*$query = "DELETE FROM sales_flat_quote WHERE reserved_order_id = ".$o->getId()."; 
                                  DELETE FROM sales_flat_invoice WHERE increment_id = ".$o->getId().";
                                  DELETE FROM sales_flat_invoice_grid WHERE order_id = ".$o->getId().";
                                  DELETE FROM sales_flat_shipment WHERE order_id = ".$o->getId().";
                                  DELETE FROM sales_flat_shipment_grid WHERE order_id = ".$o->getId().";";*/

                        $sql = "DELETE FROM ".$sales_flat_order_grid." WHERE increment_id='".mysql_escape_string($order_increment_id)."';
                                DELETE FROM ".$sales_flat_invoice_grid." WHERE order_id='".mysql_escape_string($o->getId())."'; 
                                DELETE FROM ".$sales_flat_shipment_grid." WHERE order_id='".mysql_escape_string($o->getId())."'";

                        /*$sql = "DELETE FROM ".$sales_flat_invoice_grid." WHERE order_id='".mysql_escape_string($o->getId())."'; 
                                DELETE FROM ".$sales_flat_shipment_grid." WHERE order_id='".mysql_escape_string($o->getId())."'";*/

                        //$db->query("DELETE FROM ".$sales_flat_invoice_grid." WHERE increment_id='".mysql_escape_string($order_increment_id)."'");        
                        $db->query($sql);
                        }
                        //exit;
                        //echo "deleted";
                    }   

                $this->_getSession()->addSuccess($this->__('Order was successfully deleted.'));

This code successfully deletes the order from Sales->Orders Grid, also deletes the invoice from Sales->invoices Grid, and also deletes shipment from Sales->Shipment Grid. For that, some portion it has been deleted from Database tables directly, not with magento code like ...->delete(); I need the code of Magento to delete order, invoice, shipment, also creditmemo from Grid. If any one knows about it, please reply it.

3

You'll need to load them each individually, namely:

  • Mage::getModel('sales/order')->load($order_id);
  • Mage::getModel('sales/order_invoice')->load($invoice_id);
  • Mage::getModel('sales/order_shipment')->load($shipment_id);
  • Mage::getModel('sales/order_creditmemo')->load($creditmemo_id);

The aforementioned ids are stored in the entity_id column of these tables respectively:

  • sales_flat_order,
  • sales_flat_invoice,
  • sales_flat_shipment,
  • sales_flat_creditmemo

Alternately, you will see them in the URL for the order, invoice, shipment or creditmemo page itself, /id/your_id_here/....

Once you've loaded the models, you can call the ->delete(); function on each one. Calling $order->delete(); will not delete associated invoices from your database as a method to maintain data integrity.

1
  • "I have used these code like $order = Mage::getModel('sales/order')->load($o->getId());$invoices = $order->getInvoiceCollection(); //print_r($invoices->getData()); foreach ($invoices as $invoice){ $invoiceDet = Mage::getModel('sales/order_invoice')->load($invoice->getEntityId()); $invoiceitems = $invoiceDet->getAllItems(); //print_r($invoiceitems); foreach ($invoiceitems as $invoiceitem) { $invoiceitem->delete(); } //delete invoice $invoiceDet->delete();}" But invoice is not deleting. I can not understand where I am wrong. – Ankan Bhadra Dec 23 '14 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.