Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
87 stars 33 forks source link

Fix deleting related order_id cache after delete with HPOS #451

Closed prettyboymp closed 1 year ago

prettyboymp commented 1 year ago

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 the new value instead of the previous value for the call to trigger_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 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.

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()

  1. Follow the testing instructions given in the subscriptions-core Unit Tests.
  2. 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

mattallan commented 1 year ago

I've added a quick changelog entry to record this fix, feel free to adjust before merging this :)