直接删除Magento的订单,其订单状态为pending_payment(delete magent

2019-10-17 19:46发布

在这里,我需要删除未决使用cron作业的付款状态的订单删除。 但我无法用下面的代码来获得pending_payment订单。 我想这是不是在我的代码工作

$orderCollection = Mage::getResourceModel('sales/order_collection');
            $orderCollection
                    ->addFieldToFilter('state', 'pending')

                    ->getSelect()
                    ->order('e.entity_id');

这里是我的全部代码

<?php
$mageFilename = 'app/Mage.php';

require_once $mageFilename;

Varien_Profiler::enable();

Mage::setIsDeveloperMode(true);

ini_set('display_errors', 1);

umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);
//until here you gained access to the Magento models. The rest is custom code
$orderCollection = Mage::getResourceModel('sales/order_collection');
            $orderCollection
                    ->addFieldToFilter('state', 'pending')

                    ->getSelect()
                    ->order('e.entity_id');


        echo count($orderCollection);die;
        foreach($orderCollection->getItems() as $order)
        {

                $orders_object = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('increment_id', $order['entity_id']);
                $orders_arr = $orders_object->getData();
                if(!empty($orders_arr))
                {
                            $orders_data = $orders_arr[0];
                            $orderId = $orders_data['entity_id'];//put here the id of the order you want to delete. THE ONE FROM THE DATABASE NOT THE INCREMENT_ID

                            $order = Mage::getModel('sales/order')->load($orderId);
                            $invoices = $order->getInvoiceCollection();

                              foreach ($invoices as $invoice)
                               {
                                $invoice->delete();
                               }

                         $creditnotes = $order->getCreditmemosCollection();

                                foreach ($creditnotes as $creditnote){
                                $creditnote->delete();
                                }

                         $shipments = $order->getShipmentsCollection();

                                foreach ($shipments as $shipment)
                                {
                                $shipment->delete();
                                }

                         $order->delete();

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

                                if($order_increment_id)
                                { 
                                $db->query("DELETE FROM ".$sales_flat_order_grid." WHERE increment_id='".mysql_escape_string($order_increment_id)."'");        
                                }

                  echo "deleted";
                }
                else
                {
                   echo "no record";
                }
        }
?>

Answer 1:

使用下面的代码来获取订单的挂起状态

       $order_collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('status','pending');
       foreach($order_collection as $order){
         echo '<br/>Increment ID : '.$order->getIncrementId();
         echo '<br/>STATUS : '.$order->getStatus();
       }


Answer 2:

终于我明白了。

    <?php
ini_set('display_errors', 1); 
error_reporting(E_ALL);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);

Varien_Profiler::enable();
umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);

//until here you gained access to the Magento models. The rest is custom code

$orderCollection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('status','pending_payment');
 $orderIds = array();
 $IncrementIds = array();


foreach($orderCollection as $order) {

    $orderIds[] =    $order->getEntityId();  
    $IncrementIds[] =    $order->getIncrementId();  
}



        $flag = false;
        $write = Mage::getSingleton('core/resource')->getConnection('core_write');  
        $query="show tables";
        $rsc_table=$write->fetchCol($query);    

        $table_sales_flat_order = Mage::getSingleton('core/resource')->getTableName('sales_flat_order');                        
        $table_sales_flat_creditmemo_comment= Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo_comment');
        $table_sales_flat_creditmemo_item= Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo_item');
        $table_sales_flat_creditmemo= Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo');
        $table_sales_flat_creditmemo_grid= Mage::getSingleton('core/resource')->getTableName('sales_flat_creditmemo_grid');
        $table_sales_flat_invoice_comment= Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice_comment');
        $table_sales_flat_invoice_item= Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice_item');
        $table_sales_flat_invoice= Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice');
        $table_sales_flat_invoice_grid= Mage::getSingleton('core/resource')->getTableName('sales_flat_invoice_grid');
        $table_sales_flat_quote_address_item= Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address_item');
        $table_sales_flat_quote_item_option= Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_item_option');
        $table_sales_flat_quote= Mage::getSingleton('core/resource')->getTableName('sales_flat_quote');
        $table_sales_flat_quote_address= Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
        $table_sales_flat_quote_item= Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_item');
        $table_sales_flat_quote_payment= Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_payment');
        $table_sales_flat_shipment_comment= Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_comment');
        $table_sales_flat_shipment_item= Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_item');
        $table_sales_flat_shipment_track= Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_track');
        $table_sales_flat_shipment= Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment');
        $table_sales_flat_shipment_grid= Mage::getSingleton('core/resource')->getTableName('sales_flat_shipment_grid');     
        $table_sales_flat_order_address= Mage::getSingleton('core/resource')->getTableName('sales_flat_order_address');
        $table_sales_flat_order_item= Mage::getSingleton('core/resource')->getTableName('sales_flat_order_item');
        $table_sales_flat_order_payment= Mage::getSingleton('core/resource')->getTableName('sales_flat_order_payment');
        $table_sales_flat_order_status_history= Mage::getSingleton('core/resource')->getTableName('sales_flat_order_status_history');                   
        $table_sales_flat_order_grid= Mage::getSingleton('core/resource')->getTableName('sales_flat_order_grid');                       
        $table_log_quote= Mage::getSingleton('core/resource')->getTableName('log_quote');               
        $quoteId='';    

        if (!empty($orderIds)) {
            $i=0;
            foreach ($orderIds as $orderId) {

                    $query=null;

                    if($orderId){

                        ///@jbn group order checking whether it is canceled or not//////
                        $mk_order = Mage::getModel('sales/order')->loadByIncrementId($IncrementIds[$i]);
                        $mk_items = $mk_order->getAllVisibleItems();

                        foreach ($mk_items as $mk_itemId => $mk_item)
                        {   //echo $mk_item->getSku();
                            //echo $mk_item->getStatus();
                            if($mk_item->getStatus()=="Ordered")
                            {
                                $mk_product_sku = $mk_item->getSku();
                                $mk_product_ordered_qty = $mk_item->getQtyToInvoice();
                                $mk_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$mk_product_sku);
                                 if ($mk_product) 
                                 {
                                    $mk_stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($mk_product->getId());                              

                                    $mk_stockItem->setData('qty', $mk_stockItem->getQty() + $mk_product_ordered_qty);
                                    $mk_stockItem->setData('is_in_stock',1);
                                    $mk_stockItem->save();

                                 }

                            }
                        }

                        /////end order checking////////////////////////////////

                        /*$query="show tables like 'sales_flat_order'";
                        $rs=$write->fetchAll($query);*/

                        $incId=$IncrementIds[$i];
                        if(in_array($table_sales_flat_order,$rsc_table)){
                            $query='SELECT entity_id   FROM  '.$table_sales_flat_order.'    WHERE increment_id="'.mysql_escape_string($incId).'"';

                            $rs=$write->fetchAll($query);                                               

                            $query='SELECT quote_id    FROM   '.$table_sales_flat_order.'        WHERE entity_id="'.mysql_escape_string($orderId).'"';
                            $rs1=$write->fetchAll($query);
                            $quoteId=$rs1[0]['quote_id'];
                        }

                        $query='SET FOREIGN_KEY_CHECKS=1';
                        $rs3=$write->query($query);
                        //print_r($rsc_table);
                        ////echo $table_sales_flat_creditmemo_comment;
                        if(in_array($table_sales_flat_creditmemo_comment,$rsc_table)){
                        //echo "<br />DELETE FROM ".$table_sales_flat_creditmemo_comment."    WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_creditmemo." WHERE order_id=".$orderId.")";
                        //die;
                        $write->query("DELETE FROM ".$table_sales_flat_creditmemo_comment."    WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_creditmemo." WHERE order_id='".mysql_escape_string($orderId)."')");
                        }
                        //die;


                        if(in_array('sales_flat_creditmemo_item',$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_creditmemo_item."       WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_creditmemo." WHERE order_id='".mysql_escape_string($orderId)."')";
                        $write->query("DELETE FROM ".$table_sales_flat_creditmemo_item."       WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_creditmemo." WHERE order_id='".mysql_escape_string($orderId)."')");
                        }


                        if(in_array($table_sales_flat_creditmemo,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_creditmemo."            WHERE order_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_creditmemo."            WHERE order_id='".mysql_escape_string($orderId)."'");
                        }



                        if(in_array($table_sales_flat_creditmemo_grid,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_creditmemo_grid."        WHERE order_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_creditmemo_grid."        WHERE order_id='".mysql_escape_string($orderId)."'");
                        }


                        if(in_array($table_sales_flat_invoice_comment,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_invoice_comment." WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_invoice." WHERE order_id='".mysql_escape_string($orderId)."')";

                        $write->query("DELETE FROM ".$table_sales_flat_invoice_comment." WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_invoice." WHERE order_id='".mysql_escape_string($orderId)."')");
                        }

                        if(in_array($table_sales_flat_invoice_item,$rsc_table)){
                        //echo "<br />DELETE FROM ".$table_sales_flat_invoice_item."     WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_invoice." WHERE order_id='".mysql_escape_string($orderId)."')";
                        $write->query("DELETE FROM ".$table_sales_flat_invoice_item."     WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_invoice." WHERE order_id='".mysql_escape_string($orderId)."')");
                        }


                        if(in_array($table_sales_flat_invoice,$rsc_table)){
                        //echo "<br />DELETE FROM ".$table_sales_flat_invoice."         WHERE order_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_invoice."         WHERE order_id='".mysql_escape_string($orderId)."'");
                        }

                        if(in_array($table_sales_flat_invoice_grid,$rsc_table)){
                        //echo "<br />DELETE FROM ".$table_sales_flat_invoice_grid."     WHERE order_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_invoice_grid."     WHERE order_id='".mysql_escape_string($orderId)."'");
                        }   

                        if($quoteId){                       
                            if(in_array($table_sales_flat_quote_address_item,$rsc_table)){
                                //echo "<br />DELETE FROM ".$table_sales_flat_quote_address_item."     WHERE parent_item_id  IN (SELECT address_id FROM ".$table_sales_flat_quote_address." WHERE quote_id=".$quoteId.")";                          
                            $write->query("DELETE FROM ".$table_sales_flat_quote_address_item."     WHERE parent_item_id  IN (SELECT address_id FROM ".$table_sales_flat_quote_address." WHERE quote_id=".$quoteId.")");
                            }

                            $table_sales_flat_quote_shipping_rate= Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_shipping_rate');
                            if(in_array($table_sales_flat_quote_shipping_rate,$rsc_table)){
                                //echo "<br />DELETE FROM ".$table_sales_flat_quote_shipping_rate."    WHERE address_id IN (SELECT address_id FROM ".$table_sales_flat_quote_address." WHERE quote_id=".$quoteId.")";
                            $write->query("DELETE FROM ".$table_sales_flat_quote_shipping_rate."    WHERE address_id IN (SELECT address_id FROM ".$table_sales_flat_quote_address." WHERE quote_id=".$quoteId.")");
                            }

                            if(in_array($table_sales_flat_quote_item_option,$rsc_table)){
                                //echo "<br />DELETE FROM ".$table_sales_flat_quote_item_option."     WHERE item_id IN (SELECT item_id FROM ".$table_sales_flat_quote_item." WHERE quote_id=".$quoteId.")";
                            $write->query("DELETE FROM ".$table_sales_flat_quote_item_option."     WHERE item_id IN (SELECT item_id FROM ".$table_sales_flat_quote_item." WHERE quote_id=".$quoteId.")");
                            }


                            if(in_array($table_sales_flat_quote,$rsc_table)){
                                //echo "<br />DELETE FROM ".$table_sales_flat_quote."                 WHERE entity_id=".$quoteId;
                            $write->query("DELETE FROM ".$table_sales_flat_quote."                 WHERE entity_id=".$quoteId);
                            }

                            if(in_array($table_sales_flat_quote_address,$rsc_table)){
                                //echo "<br />DELETE FROM ".$table_sales_flat_quote_address."         WHERE quote_id=".$quoteId;
                            $write->query("DELETE FROM ".$table_sales_flat_quote_address."         WHERE quote_id=".$quoteId);
                            }

                            if(in_array($table_sales_flat_quote_item,$rsc_table)){
                                //echo "<br />DELETE FROM ".$table_sales_flat_quote_item."             WHERE quote_id=".$quoteId;
                            $write->query("DELETE FROM ".$table_sales_flat_quote_item."             WHERE quote_id=".$quoteId);
                            }

                            if(in_array('sales_flat_quote_payment',$rsc_table)){
                                //echo "<br />DELETE FROM ".$table_sales_flat_quote_payment."         WHERE quote_id=".$quoteId;
                            $write->query("DELETE FROM ".$table_sales_flat_quote_payment."         WHERE quote_id=".$quoteId);
                            }

                        }


                        if(in_array($table_sales_flat_shipment_comment,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_shipment_comment."    WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_shipment." WHERE order_id='".mysql_escape_string($orderId)."')";
                        $write->query("DELETE FROM ".$table_sales_flat_shipment_comment."    WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_shipment." WHERE order_id='".mysql_escape_string($orderId)."')");
                        }

                        if(in_array($table_sales_flat_shipment_item,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_shipment_item."         WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_shipment." WHERE order_id='".mysql_escape_string($orderId)."')";
                        $write->query("DELETE FROM ".$table_sales_flat_shipment_item."         WHERE parent_id IN (SELECT entity_id FROM ".$table_sales_flat_shipment." WHERE order_id='".mysql_escape_string($orderId)."')");
                        }


                        if(in_array($table_sales_flat_shipment_track,$rsc_table)){  
                        //echo "<br />DELETE FROM ".$table_sales_flat_shipment_track."         WHERE order_id  IN (SELECT entity_id FROM ".$table_sales_flat_shipment." WHERE order_id='".mysql_escape_string($orderId)."')";                   
                        $write->query("DELETE FROM ".$table_sales_flat_shipment_track."         WHERE order_id  IN (SELECT entity_id FROM ".$table_sales_flat_shipment." WHERE order_id='".mysql_escape_string($orderId)."')");
                        }


                        if(in_array($table_sales_flat_shipment,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_shipment."             WHERE order_id='".mysql_escape_string($orderId)."'";

                        $write->query("DELETE FROM ".$table_sales_flat_shipment."             WHERE order_id='".mysql_escape_string($orderId)."'");
                        }


                        if(in_array($table_sales_flat_shipment_grid,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_shipment_grid."         WHERE order_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_shipment_grid."         WHERE order_id='".mysql_escape_string($orderId)."'");
                        }

                        if(in_array($table_sales_flat_order,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_order."                     WHERE entity_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_order."                     WHERE entity_id='".mysql_escape_string($orderId)."'");
                        }

                        if(in_array($table_sales_flat_order_address,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_order_address."            WHERE parent_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_order_address."            WHERE parent_id='".mysql_escape_string($orderId)."'");
                        }

                        if(in_array($table_sales_flat_order_item,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_order_item."                 WHERE order_id='".mysql_escape_string($orderId)."'";                     
                        $write->query("DELETE FROM ".$table_sales_flat_order_item."                 WHERE order_id='".mysql_escape_string($orderId)."'");
                        }
                        if(in_array($table_sales_flat_order_payment,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_order_payment."             WHERE parent_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_order_payment."             WHERE parent_id='".mysql_escape_string($orderId)."'");
                        }
                        if(in_array($table_sales_flat_order_status_history,$rsc_table)){
                            //echo "<br />DELETE FROM ".$table_sales_flat_order_status_history."     WHERE parent_id='".mysql_escape_string($orderId)."'";
                        $write->query("DELETE FROM ".$table_sales_flat_order_status_history."     WHERE parent_id='".mysql_escape_string($orderId)."'");
                        }
                        if($incId&&in_array($table_sales_flat_order_grid,$rsc_table)){  
                        //echo "<br />DELETE FROM ".$table_sales_flat_order_grid."                 WHERE increment_id='".mysql_escape_string($incId)."'";                   
                            $write->query("DELETE FROM ".$table_sales_flat_order_grid."                 WHERE increment_id='".mysql_escape_string($incId)."'");

                        }

                        $query="show tables like '%".$table_log_quote."'";
                        //echo $query;
                        $rsc_table_l=$write->fetchCol($query);  
                        if($quoteId&&$rsc_table_l){ 
                        //echo "<br />DELETE FROM ".$table_log_quote." WHERE quote_id=".$quoteId;                   
                                $write->query("DELETE FROM ".$table_log_quote." WHERE quote_id=".$quoteId);                         
                        }
                        $write->query("SET FOREIGN_KEY_CHECKS=1");                      
                    }                   
            $i++;}

        }
 ///////////end



?>


文章来源: delete magento order directly which order status is pending_payment