如何从Magento2中删除测试订单

时间:2016-06-16 13:41:55

标签: magento magento2 orders

请解释我在Magento2网站上删除测试订单的正确方法。我删除了' sales_order'中的所有记录。表,但后端仍然存在订单。

6 个答案:

答案 0 :(得分:42)

在Magento 2.1.0中测试

安全:首先备份你的sql。

根据您的需要使用下面的SQL:

SET FOREIGN_KEY_CHECKS=0;

# Clean order history
TRUNCATE TABLE `sales_bestsellers_aggregated_daily`;
TRUNCATE TABLE `sales_bestsellers_aggregated_monthly`;
TRUNCATE TABLE `sales_bestsellers_aggregated_yearly`;

# Clean order infos
TRUNCATE TABLE `sales_creditmemo`;
TRUNCATE TABLE `sales_creditmemo_comment`;
TRUNCATE TABLE `sales_creditmemo_grid`;
TRUNCATE TABLE `sales_creditmemo_item`;
TRUNCATE TABLE `sales_invoice`;
TRUNCATE TABLE `sales_invoiced_aggregated`;
TRUNCATE TABLE `sales_invoiced_aggregated_order`;
TRUNCATE TABLE `sales_invoice_comment`;
TRUNCATE TABLE `sales_invoice_grid`;
TRUNCATE TABLE `sales_invoice_item`;
TRUNCATE TABLE `sales_order`;
TRUNCATE TABLE `sales_order_address`;
TRUNCATE TABLE `sales_order_aggregated_created`;
TRUNCATE TABLE `sales_order_aggregated_updated`;
TRUNCATE TABLE `sales_order_grid`;
TRUNCATE TABLE `sales_order_item`;
TRUNCATE TABLE `sales_order_payment`;
TRUNCATE TABLE `sales_order_status_history`;
TRUNCATE TABLE `sales_order_tax`;
TRUNCATE TABLE `sales_order_tax_item`;
TRUNCATE TABLE `sales_payment_transaction`;
TRUNCATE TABLE `sales_refunded_aggregated`;
TRUNCATE TABLE `sales_refunded_aggregated_order`;
TRUNCATE TABLE `sales_shipment`;
TRUNCATE TABLE `sales_shipment_comment`;
TRUNCATE TABLE `sales_shipment_grid`;
TRUNCATE TABLE `sales_shipment_item`;
TRUNCATE TABLE `sales_shipment_track`;
TRUNCATE TABLE `sales_shipping_aggregated`;
TRUNCATE TABLE `sales_shipping_aggregated_order`;

# Clean cart infos
TRUNCATE TABLE `quote`;
TRUNCATE TABLE `quote_address`;
TRUNCATE TABLE `quote_address_item`;
TRUNCATE TABLE `quote_id_mask`;
TRUNCATE TABLE `quote_item`;
TRUNCATE TABLE `quote_item_option`;
TRUNCATE TABLE `quote_payment`;
TRUNCATE TABLE `quote_shipping_rate`;

# Reset indexes (if you want your orders number start back to 1
TRUNCATE TABLE sequence_invoice_1;
TRUNCATE TABLE sequence_order_1;
TRUNCATE TABLE sequence_shipment_1;
TRUNCATE TABLE sequence_creditmemo_1;


SET FOREIGN_KEY_CHECKS=1;

请勿截止/清空以下内容:

  • sales_order_status
  • sales_sequence_meta
  • sales_sequence_profile
  • sales_order_status_label
  • sales_order_status_state

答案 1 :(得分:3)

您可以通过编程方式删除订单:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$order = $objectManager->create('Magento\Sales\Model\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());

//delete all order items

$items = $order->getAllItems();

foreach ($items as $item) {

    $item->delete();

}

$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();
}

答案 2 :(得分:0)

为什么不使用Magento2免费提供的这个简单扩展程序 https://magecomp.com/magento-2-delete-orders.html

答案 3 :(得分:0)

这在Magento 2.3.1中也有效。除了user1171440的答案外,我还提到了重置/截断测试客户数据的可能性:

SET FOREIGN_KEY_CHECKS=0;
TRUNCATE TABLE `customer_address_entity`;
TRUNCATE TABLE `customer_address_entity_datetime`;
TRUNCATE TABLE `customer_address_entity_decimal`;
TRUNCATE TABLE `customer_address_entity_int`;
TRUNCATE TABLE `customer_address_entity_text`;
TRUNCATE TABLE `customer_address_entity_varchar`;
TRUNCATE TABLE `customer_entity`;
TRUNCATE TABLE `customer_entity_datetime`;
TRUNCATE TABLE `customer_entity_decimal`;
TRUNCATE TABLE `customer_entity_int`;
TRUNCATE TABLE `customer_entity_text`;
TRUNCATE TABLE `customer_entity_varchar`;
TRUNCATE TABLE `customer_grid_flat`;
TRUNCATE TABLE `customer_visitor`;
SET FOREIGN_KEY_CHECKS=1;

答案 4 :(得分:-1)

SET FOREIGN_KEY_CHECKS=0;

TRUNCATE TABLE sales_order  ;
TRUNCATE TABLE sales_order_grid ;
TRUNCATE TABLE sales_invoice ;
TRUNCATE TABLE sales_invoice_grid ;
TRUNCATE TABLE sales_creditmemo ;
TRUNCATE TABLE sales_creditmemo_grid ;
TRUNCATE TABLE sales_shipment ;
TRUNCATE TABLE sales_shipment_grid ;

SET FOREIGN_KEY_CHECKS=1;

答案 5 :(得分:-2)

从Magento2中删除测试订单 在Mysql查询下面运行:

TRUNCATE TABLE sales_order  ;
TRUNCATE TABLE sales_order_grid ;
TRUNCATE TABLE sales_invoice ;
TRUNCATE TABLE sales_invoice_grid ;
TRUNCATE TABLE sales_creditmemo ;
TRUNCATE TABLE sales_creditmemo_grid ;
TRUNCATE TABLE sales_shipment ;
TRUNCATE TABLE sales_shipment_grid ;