Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
85 stars 31 forks source link

Update WCS_Related_Order_Store_Cached_CPT to use an object cache manager on HPOS environments #373

Closed james-allan closed 1 year ago

james-allan commented 1 year ago

Fixes #339

Description

In https://github.com/Automattic/woocommerce-subscriptions-core/pull/304 I introduced a new cache manager that works with CRUD objects rather than hooking onto post related hooks.

As brief explainer, the cache managers essentially listen for changes to specific objects (posts or crud objects) for changes to specific meta keys or properties. When a relevant change occurs, the cache manager's job is to trigger a hook to let the cache's know that the cache needs to be updated.

Prior to this PR the WCS_Related_Order_Store_Cached_CPT cache still used the old WCS_Post_Meta_Cache_Manager which meant changes to related order meta flags (eg subscription_renewal) on HPOS environments wouldn't be picked up by the WCS_Post_Meta_Cache_Manager, and so the subscription's related order cache wouldn't be updated.

This PR fixes that.

Note This PR mostly impacts 3rd parties who set or change related order meta via update_meta() rather than use the related order APIs like WCS_Related_Order_Store::instance()->add_relation()

How to test this PR

  1. Enable HPOS tables.
  2. Run the following code snippets.
  3. After running each snippet, observe the subscription related order in the UI and in the database to ensure the expected outcome is achieved.
  4. Enable CPT tables.
  5. Run the following code snippets.
  6. After running each snippet, observe the subscription related order in the UI and in the database to ensure the expected outcome is achieved.

Code snippets for testing

New order

$order = new WC_Order();
$order->update_meta_data( '_subscription_renewal', 925 ); // <- Where 925 is a subscription ID.
$order->save();

Expectation: A newly created order is created. The subscription 925 has the new order in its renewal order related cache.

Change order

$order = wc_get_order( 123 ); // <- where 123 is the order you created in the last test (a renewal order).
$order->update_meta_data( '_subscription_renewal', 100 ); // <- Where 925 is a different subscription ID.
$order->save();

Expectation: The order (123) changes from being a renewal order for 925, to being a renewal order for 927. Subscription 925 shouldn't have the renewal order 123 in its cache, 927 should.

Delete relation

$order = wc_get_order( 123 ); // <- where 123 is the order you created in the last test (a renewal order).
$order->delete_meta_data( '_subscription_renewal' ); // <- Where 925 is a different subscription ID.
$order->save();

Expectation: The order (123) changes from being a renewal order for 925, to no longer being a renewal order at all.

Product impact