With HPOS enabled, the cached order relationships for subscriptions is not updated when the order is deleted.
It does properly update without HPOS enabled.
There are two bugs here causing the issue.
First, the WCS_Object_Data_Cache_Manager::deleted() attempts to pass in the new value instead of the previous value for the call to trigger_update_cache_hook().
This means that WCS_Related_Order_Store_Cached_CPT::maybe_update_for_post_meta_change() doesn't receive a $subscription_id and call WCS_Related_Order_Store_Cached_CPT::delete_related_order_id_from_caches() to delete the order_id from all subscription relationships. However, ::delete_related_order_id_from_caches() fails to retrieve any related subscriptions for the order because the order is already deleted at this point.
Set the HPOS environmental variable when running the test: $> HPOS=1 ./vendor/bin/phpunit --filter=WCS_Related_Order_Store_Cached_CPT_Test::test_cache_updates_after_related_object_is_deleted_one_to_many
Product impact
[ ] Added changelog entry (or does not apply)
[ ] Will this PR affect WooCommerce Subscriptions? yes/no/tbc, add issue ref
[ ] Will this PR affect WooCommerce Payments? yes/no/tbc, add issue ref
[ ] Added deprecated functions, hooks or classes to the spreadsheet
Description
With HPOS enabled, the cached order relationships for subscriptions is not updated when the order is deleted.
It does properly update without HPOS enabled.
There are two bugs here causing the issue.
First, the
WCS_Object_Data_Cache_Manager::deleted()
attempts to pass in thenew
value instead of theprevious
value for the call totrigger_update_cache_hook()
.https://github.com/Automattic/woocommerce-subscriptions-core/blob/39fc2b3394f207154fd29bbf78b7b3ad41b889b4/includes/class-wcs-object-data-cache-manager.php#L285
This means that
WCS_Related_Order_Store_Cached_CPT::maybe_update_for_post_meta_change()
doesn't receive a$subscription_id
and callWCS_Related_Order_Store_Cached_CPT::delete_related_order_id_from_caches()
to delete the order_id from all subscription relationships. However,::delete_related_order_id_from_caches()
fails to retrieve any related subscriptions for the order because the order is already deleted at this point.https://github.com/Automattic/woocommerce-subscriptions-core/blob/39fc2b3394f207154fd29bbf78b7b3ad41b889b4/includes/wcs-order-functions.php#L40
How to test this PR
New unit test added:
WCS_Related_Order_Store_Cached_CPT_Test::test_cache_updates_after_related_object_is_deleted_one_to_many()
$> HPOS=1 ./vendor/bin/phpunit --filter=WCS_Related_Order_Store_Cached_CPT_Test::test_cache_updates_after_related_object_is_deleted_one_to_many
Product impact