[COT] Fix inappropriate deletion of order from posts with sync off (#36617)

* Fix inappropriate deletion of order from posts with sync off

When the orders table is authoritative and sync is off, deleting
and order should not delete the order record from the posts table,
this commit fixes that.

Additionally, OrdersTableDataStore::delete will now trigger the
woocommerce_delete_order action in all cases, even when the
method is called while the posts table is authoritative.

* Add changelog file

* Fix: order items were not deleted

* Improve comment in the 'delete' method

* Use delete_items instead of a dedicated method for deleting order items
This commit is contained in:
Néstor Soriano 2023-01-30 21:04:45 +01:00 committed by GitHub
parent 6c053d3eec
commit f601b0d8a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 7 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Don't delete order from posts table when deleted from orders table if the later is authoritative and sync is off

View File

@ -1763,18 +1763,18 @@ FROM $order_meta_table
$this->upshift_child_orders( $order );
$this->delete_order_data_from_custom_order_tables( $order_id );
$this->delete_items( $order );
$order->set_id( 0 );
// If this datastore method is called while the posts table is authoritative, refrain from deleting post data.
if ( $order->get_data_store()->get_current_class_name() !== self::class ) {
return;
// Only delete post data if the posts table is authoritative and synchronization is enabled.
$data_synchronizer = wc_get_container()->get( DataSynchronizer::class );
if ( $data_synchronizer->data_sync_is_enabled() && $order->get_data_store()->get_current_class_name() === self::class ) {
// Delete the associated post, which in turn deletes order items, etc. through {@see WC_Post_Data}.
// Once we stop creating posts for orders, we should do the cleanup here instead.
wp_delete_post( $order_id );
}
// Delete the associated post, which in turn deletes order items, etc. through {@see WC_Post_Data}.
// Once we stop creating posts for orders, we should do the cleanup here instead.
wp_delete_post( $order_id );
do_action( 'woocommerce_delete_order', $order_id ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
} else {
/**

View File

@ -8,6 +8,8 @@ use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableQuery;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
use Automattic\WooCommerce\RestApi\UnitTests\HPOSToggleTrait;
require_once __DIR__ . '/../../../../helpers/HPOSToggleTrait.php';
/**
* Class OrdersTableDataStoreTests.
*