Set child orders to be children of current order parent before deleting. (#36218)

This commit is contained in:
Vedanshu Jain 2023-01-05 15:24:49 +05:30 committed by GitHub
commit dab19a3ec9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Set child orders to be children of current order parent before deleting for consistency.

View File

@ -1761,7 +1761,9 @@ FROM $order_meta_table
*/
do_action( 'woocommerce_before_delete_order', $order_id, $order );
$this->upshift_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.
@ -1791,6 +1793,26 @@ FROM $order_meta_table
}
}
/**
* Helper method to set child orders to the parent order's parent.
*
* @param \WC_Abstract_Order $order Order object.
*
* @return void
*/
private function upshift_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.
*

View File

@ -1899,6 +1899,25 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case {
$this->assertFalse( $should_sync_callable->call( $this->sut, $order ) );
}
/**
* @testDox When parent order is deleted, child orders should be upshifted.
*/
public function test_child_orders_are_promoted_when_parent_is_deleted() {
$this->toggle_cot( true );
$order = new WC_Order();
$order->save();
$child_order = new WC_Order();
$child_order->set_parent_id( $order->get_id() );
$child_order->save();
$this->assertEquals( $order->get_id(), $child_order->get_parent_id() );
$this->sut->delete( $order, array( 'force_delete' => true ) );
$child_order = wc_get_order( $child_order->get_id() );
$this->assertEquals( 0, $child_order->get_parent_id() );
}
/**
* @testDox Make sure get_order return false when checking an order of different order types without warning.
*/