From 44757082d090d03abb4b95656108c9e17ae74e34 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Wed, 28 Dec 2022 16:09:13 +0530 Subject: [PATCH] Set child orders to be children of current order parent before deleting for consistency. In post storage, just before deleting an order, we will change all child order's parent to be parent of current order. This commit adds the same behavior for HPOS for consistency. --- .../DataStores/Orders/OrdersTableDataStore.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php index f2543f037f6..ace23ce36e9 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php @@ -1766,7 +1766,9 @@ FROM $order_meta_table */ do_action( 'woocommerce_before_delete_order', $order_id, $order ); + $this->unlink_child_orders( $order ); $this->delete_order_data_from_custom_order_tables( $order_id ); + $order->set_id( 0 ); // If this datastore method is called while the posts table is authoritative, refrain from deleting post data. @@ -1796,6 +1798,19 @@ FROM $order_meta_table } } + private function unlink_child_orders( $order ) { + global $wpdb; + $order_table = self::get_orders_table_name(); + $order_parent = $order->get_parent_id(); + $wpdb->update( + $order_table, + array( 'parent_order_id' => $order_parent ), + array( 'parent_order_id' => $order->get_id() ), + array( '%d' ), + array( '%d' ) + ); + } + /** * Trashes an order. *