Automattic / woocommerce-subscriptions-core

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

Fix duplicate related order caches after HPOS post backfill #540

Closed james-allan closed 10 months ago

james-allan commented 10 months ago

Description

Resolves issues raised in https://github.com/Automattic/woocommerce-subscriptions-core/pull/538#issuecomment-1801122335

WC core includes a bug (https://github.com/woocommerce/woocommerce/pull/41281) that has two affects when attempting to backfill subscription related order meta. If the cache is empty it wouldn't copy the meta at all. If the cache had values, it would duplicate. The missing meta was resolved by #538 however that meant it was now always duplicating the meta on the latest released version of WC.

I've submitted a fix to WC core (https://github.com/woocommerce/woocommerce/pull/41281), however while we wait for that to be released we want to prevent those duplicate cache meta values from appearing after a backfill.

This PR fixes that by making sure we delete all instances of a subscriptions related order cache after the WC part of the backfill is completed before we resave a final-single copy of the cache.

The flow is as follows:

  1. WC duplicates the meta in update_order_meta_from_object() called here
  2. WC core calls the datastore getters and setters.
  3. That calls the functions I've changed in this PR.
  4. We delete all duplicates previously added by core and then save a single copy of the cache.

How to test this PR

  1. Make sure you're using the latest version of WC - 8.2.1
  2. Go WC > Settings > Advanced > Features and enable HPOS without compatibility mode
  3. Purchase 2 different subscription products (we want 2 subscriptions).
  4. Populate the renewal order IDs cache meta by processing a renewal order for 1 of the subscriptions
  5. Open the HPOS settings again and enable compatibility mode and save the settings
  6. If the sync process is slow to kick-off here are some steps to manually run it:
    1. Run the wc_schedule_pending_batch_processes pending scheduled action
    2. Then run the new wc_run_batch_process scheduled action
  7. Open your DB and search wp_postmeta for the subscription ID
    • On fix/hpos-dates-cache-not-backfilled you should see duplicate related order caches.
    • This branch there should be no duplicates.
fix/hpos-dates-cache-not-backfilled This Branch
Screenshot 2023-11-08 at 4 54 35 pm
Duplicates
Screenshot 2023-11-08 at 4 51 52 pm
No duplicates

Product impact