01.11.10

Magento – Delete Order (inkl. Invoice etc.)

Posted in Magento at 6:09 pm by alewo

Das Löschen von Testbestellungen ist leider in Magento standardmäßig nicht vorgesehen, daher muss man sich im Moment leider noch selbst darum zu kümmern. Zum Glück gibt es mit der Delete Orders Extension eine Erweiterung, die einem die meiste Arbeit abnimmt, jedoch ist sie nicht vollkommen und lässt einen im Stich, wenn man Bestellungen löschen will, für die man eine Rechnung erstellt hat.

Unglücklicherweise bleiben also noch einige Bestellungen bestehen. Da ich natürlich nicht der erste bin, der damit Probleme hat, gibt es auch schon einen schönen Thread im Magento-Forum dazu.
Hier ist eine schöne Lösung der Problematik beschrieben, die aber mitunter bei einigen Installationen wegen der Kollation zu Problemen führt. Deswegen ist es nötig, die dort beschriebene Lösung anzupassen.
Auch die Temporary Tables die erstellt werden, werden in der Lösung hier als Tables erstellt, da ich nicht die Möglichkeit zum Grant der entsprechenden Rechte hatte.

Also eine Lösung die bei mir funktioniert und bei der man nur bei @orderid die ID der Bestellung die gelöscht werden soll setzen muss:

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
85
86
87
88
89
90
91
92
93
94
95
SET @orderId = '100000001' COLLATE 'utf8_general_ci';
 
SET FOREIGN_KEY_CHECKS = 1;
 
SET @salesFlatQuoteId = (
    SELECT entity_id
      FROM sales_flat_quote
     WHERE reserved_order_id = @orderId
);
 
SET @salesOrderId = (
    SELECT entity_id
      FROM sales_order
     WHERE increment_id = @orderId
);
 
/* temp table used as an array */
CREATE TABLE del_sales(
    id      INT AUTO_INCREMENT PRIMARY KEY,
    salesId INT(10)
) 
COLLATE 'utf8_general_ci';
 
/* temp table used as an array */
CREATE TABLE del_statusSales(
    id      INT AUTO_INCREMENT PRIMARY KEY,
    salesId INT(10)
) COLLATE 'utf8_general_ci';
 
INSERT INTO del_statusSales (salesId)
    SELECT entity_id
      FROM sales_order_entity_int
     WHERE VALUE = @salesOrderId
       AND attribute_id = ANY (
        SELECT attribute_id
          FROM eav_attribute
         WHERE attribute_code = 'order_id'
    )
       AND entity_id = ANY (
        SELECT entity_id
          FROM sales_order_entity
         WHERE entity_type_id = ANY (
            SELECT entity_type_id
              FROM eav_entity_type
             WHERE entity_type_code = 'invoice'
                OR entity_type_code = 'shipment'
                OR entity_type_code = 'creditmemo'
        )
    );
 
INSERT INTO del_sales (salesId)
    SELECT entity_id
      FROM sales_order_entity
     WHERE parent_id = ANY (
        SELECT salesId
          FROM del_statusSales
    )
       AND entity_type_id = ANY (
        SELECT entity_type_id
          FROM eav_entity_type
          WHERE entity_type_code = 'invoice_item'
             OR entity_type_code = 'invoice_comment'
             OR entity_type_code = 'shipment_item'
             OR entity_type_code = 'shipment_comment'
             OR entity_type_code = 'shipment_track'
             OR entity_type_code = 'creditmemo_item'
             OR entity_type_code = 'creditmemo_comment'
    );
INSERT INTO del_sales (salesId)
    SELECT salesId
      FROM del_statusSales;
 
INSERT INTO del_sales (salesId)
    SELECT entity_id
      FROM sales_order_entity
     WHERE parent_id = @salesOrderId;
 
DELETE FROM sales_order_entity
      WHERE entity_id = ANY (
    SELECT salesId
      FROM del_sales
);
 
DELETE FROM sales_flat_quote
      WHERE reserved_order_id = @orderId;
 
DELETE FROM sales_flat_order_item
      WHERE quote_item_id = @salesFlatQuoteId;
 
DELETE FROM sales_order
      WHERE increment_id = @orderId;
 
/* drop temp tables */
DROP TABLE del_sales;
DROP TABLE del_statusSales;

Leave a Comment

You must be logged in to post a comment.