-
Notifications
You must be signed in to change notification settings - Fork 0
/
deleteOrder.php
84 lines (63 loc) · 2.24 KB
/
deleteOrder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* Delete Order, Invoices, Shipments and Creditmemos programatically
* with standalone php file using magento bootstrap.
*
* Tested on Magento 2.2.6 and 2.3.1
*
* Using $_debugMode for test before.
*
*/
ini_set('error_reporting', E_ALL);
ini_set("display_errors", "1");
ini_set("memory_limit","1024M");
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$registry = $objectManager->get('Magento\Framework\Registry');
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$_debugMode = true;
$ids = array(1,2,3,4); // your order_id
echo "<pre>Begin... <br />";
if($_debugMode){
echo "Debug mode is ENABLED. Orders don't be deleted. <br />";
}
foreach ($ids as $id) {
echo "--------------------------------------------------------- <br/>";
$order = $objectManager->create('Magento\Sales\Model\Order')->load($id);
if(!empty($order->getId())){
$registry->register('isSecureArea','true');
// Delete all invoices from Order.
$_invoices = $order->getInvoiceCollection();
if($_invoices){
foreach($_invoices as $invoice){
(!$_debugMode) ? $invoice->delete() : '';
echo "InvoiceID:".$invoice->getId()."<br />";
}
}
// Delete all Shipments from Order.
$_shipments = $order->getShipmentsCollection();
if($_shipments){
foreach($_shipments as $shipment){
(!$_debugMode) ? $shipment->delete() : '';
echo "ShipmentID:".$shipment->getId()."<br />";
}
}
// Delete all CreditMemos from Order.
$_creditmemos = $order->getCreditmemosCollection();
if($_creditmemos){
foreach($_creditmemos as $creditmemo){
(!$_debugMode) ? $creditmemo->delete() : '';
echo "CreditMemoID:".$creditmemo->getId()."<br />";
}
}
(!$_debugMode) ? $order->delete() : '';
$registry->unregister('isSecureArea');
echo "Order ".$id." deleted <br />";
}else{
echo "Order $id not found. <br/>";
}
}
echo "<br />Finished! <br /></pre>";