如何从 Magento2 中删除测试订单

Posted

技术标签:

【中文标题】如何从 Magento2 中删除测试订单【英文标题】:How to remove test orders from Magento2 【发布时间】:2016-10-18 01:55:42 【问题描述】:

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

【问题讨论】:

最好和简单易用的扩展,magecomp.com/magento-2-delete-orders.html 是的,我同意并且我更喜欢使用更好的删除订单扩展github.com/mageplaza/magento-2-delete-orders 【参考方案1】:

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

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

【讨论】:

这里为什么使用 Magento1 代码? $order = Mage::getModel('sales/order')->load($o->getId()); 加上这会删除商品、发票等,但不会删除 sales_order 表中的订单记录。异常#0(Magento\Framework\Exception\LocalizedException):当前区域禁止删除操作我们是否应该从sales_order表中手动删除订单记录?请指导。【参考方案2】:

这也适用于 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;

【讨论】:

【参考方案3】:
SET FOREIGN_KEY_CHECKS=0;

# Clean order history
TRUNCATE TABLE `mg_sales_bestsellers_aggregated_daily`;
TRUNCATE TABLE `mg_sales_bestsellers_aggregated_monthly`;
TRUNCATE TABLE `mg_sales_bestsellers_aggregated_yearly`;

# Clean order infos
TRUNCATE TABLE `mg_sales_creditmemo`;
TRUNCATE TABLE `mg_sales_creditmemo_comment`;
TRUNCATE TABLE `mg_sales_creditmemo_grid`;
TRUNCATE TABLE `mg_sales_creditmemo_item`;
TRUNCATE TABLE `mg_sales_invoice`;
TRUNCATE TABLE `mg_sales_invoiced_aggregated`;
TRUNCATE TABLE `mg_sales_invoiced_aggregated_order`;
TRUNCATE TABLE `mg_sales_invoice_comment`;
TRUNCATE TABLE `mg_sales_invoice_grid`;
TRUNCATE TABLE `mg_sales_invoice_item`;
TRUNCATE TABLE `mg_sales_order`;
TRUNCATE TABLE `mg_sales_order_address`;
TRUNCATE TABLE `mg_sales_order_aggregated_created`;
TRUNCATE TABLE `mg_sales_order_aggregated_updated`;
TRUNCATE TABLE `mg_sales_order_grid`;
TRUNCATE TABLE `mg_sales_order_item`;
TRUNCATE TABLE `mg_sales_order_payment`;
TRUNCATE TABLE `mg_sales_order_status_history`;
TRUNCATE TABLE `mg_sales_order_tax`;
TRUNCATE TABLE `mg_sales_order_tax_item`;
TRUNCATE TABLE `mg_sales_payment_transaction`;
TRUNCATE TABLE `mg_sales_refunded_aggregated`;
TRUNCATE TABLE `mg_sales_refunded_aggregated_order`;
TRUNCATE TABLE `mg_sales_shipment`;
TRUNCATE TABLE `mg_sales_shipment_comment`;
TRUNCATE TABLE `mg_sales_shipment_grid`;
TRUNCATE TABLE `mg_sales_shipment_item`;
TRUNCATE TABLE `mg_sales_shipment_track`;
TRUNCATE TABLE `mg_sales_shipping_aggregated`;
TRUNCATE TABLE `mg_sales_shipping_aggregated_order`;

# Clean cart infos
TRUNCATE TABLE `mg_quote`;
TRUNCATE TABLE `mg_quote_address`;
TRUNCATE TABLE `mg_quote_address_item`;
TRUNCATE TABLE `mg_quote_id_mask`;
TRUNCATE TABLE `mg_quote_item`;
TRUNCATE TABLE `mg_quote_item_option`;
TRUNCATE TABLE `mg_quote_payment`;
TRUNCATE TABLE `mg_quote_shipping_rate`;

# Reset indexes (if you want your orders number start back to 1
TRUNCATE TABLE mg_sequence_invoice_1;
TRUNCATE TABLE mg_sequence_order_1;
TRUNCATE TABLE mg_sequence_shipment_1;
TRUNCATE TABLE mg_sequence_creditmemo_1;


SET FOREIGN_KEY_CHECKS=1;

【讨论】:

【参考方案4】:

在 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

【讨论】:

这个答案会比现在选择的更全面吗? 这个脚本对我不起作用(magento2 CE 2.2.1)。 TRUNCATE TABLE sales_creditmemo mysql 说:文档 #1701 - 无法截断在外键约束中引用的表 (fabtablabtest_preinstalled_magento.sales_creditmemo_comment, CONSTRAINT SALES_CREDITMEMO_COMMENT_PARENT_ID_SALES_CREDITMEMO_ENTITY_ID FOREIGN KEY (parent_id) REFERENCES `fabtablabtest_) @MichelTol 你找到 2.2.1 的解决方案了吗? @MichelTol 如果您使用的是 phpMyAdmin,则必须取消选中粘贴 SQL 的文本区域下方的“启用外键检查”框。 注意!如果您使用超过 1 个商店,您还应该截断 sequence_invoice_2...X 个表【参考方案5】:
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;

【讨论】:

运行这个查询会给你带来麻烦。它是不完整的。我强烈建议不要使用此查询。例如,sales_order_tax_item 表不会被截断。这将导致新订单(与旧订单具有相同的 id)在此表中有几行(包括旧订单中的错误行)。因此,新订单商品的税务信息将是错误的。【参考方案6】:

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

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 ;

【讨论】:

以上是关于如何从 Magento2 中删除测试订单的主要内容,如果未能解决你的问题,请参考以下文章

sql 截断Magento2订单和客户数据

Magento 2在结帐页面phtml中发送到支付网关之前获取订单ID?

Magento2 - 使用 Paypal 计费协议创建自定义订单(“强制参数缺少 referenceId 错误”)

Magento2 - 使用Paypal结算协议创建自定义订单(“强制参数缺少referenceId错误”)

Magento 2.3 发货

Magento 2.3.2 PayPal Payflow 付款失败