Automattic / woocommerce-subscriptions-core

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

Clear subscription cache after writing dates to the database #577

Closed mattallan closed 6 months ago

mattallan commented 6 months ago

Fixes https://github.com/woocommerce/woocommerce-subscriptions/issues/4620

Description

This PR addresses a caching issue identified while adding Subscriptions compatibility to our Pre-Orders extension. What was found was that updating the date of a subscription did not refresh the cache, resulting in outdated dates being displayed, particularly in customer emails.

This issue was caused by our datastore class not clearing the cache inside save_dates() or write_dates_to_database(). Our other add, update and delete metadata functions already clear the cache thanks to piggy-backing off of WooCommerce Core code. For reference, you can see the cache being cleared inside the following functions:

I couldn't replicate the error when subscriptions are stored in the WP Posts table, therefore this PR only changes the HPOS datastore class.

How to test this PR

  1. Enable HPOS and purchase a subscription product to create a fresh subscription
  2. Check out trunk of subscriptions-core
  3. Using something like WP Console, run the following snippet of code:
    
    // Update the subscriptions start date
    $subscription = wcs_get_subscription( 123 );
    $subscription->update_dates( [ 'start_date' => '2020-01-01 00:00:00' ] );

// Get a fresh copy of the subscription $subscription = wcs_get_subscription( 123 ); echo $subscription->get_date( 'start' );


4. While on `trunk`, notice the outdated start date is returned (due to the subscription object being fetched from cache)
5. Checkout `issue/4620`, update the snippet to a different start date again
6. Run the snippet and confirm a fresh copy of the subscription is being returned and the correct start date is being echoed.
7. Disable HPOS and confirm the caching issue is not present.

## Product impact
<!-- What products will this PR ship in? -->

- [x] 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
- [ ] <!-- šŸšØ Deprecations šŸšØ --> Added deprecated functions, hooks or classes to the [spreadsheet](https://docs.google.com/spreadsheets/d/1xw9xszcPMnWsp4C8OKZMsLzZob7tOmWT7qMqmEIq314/edit#gid=0)
peterwilsoncc commented 6 months ago

@mattallan @james-allan Is this likely to be in subscriptions core 6.9.0 along with the filter I added in the other PR. I'm just trying to figure out if I'll need to retain the fix I have in pre-orders? Also, do sub and subs-core share the same version numbers?

james-allan commented 6 months ago

Is this likely to be in subscriptions core 6.9.0 along with the filter I added in the other PR.

Yep. This and https://github.com/Automattic/woocommerce-subscriptions-core/pull/579 will be included in the 6.9.0 tag later this week.

Also, do sub and subs-core share the same version numbers?

No they don't share version numbers. The release of Woo Subscriptions later this week will be 6.1.0. Subscriptions-core versions are essentially irrelevant now because the feature in WooPayments has been deprecated and so third-parties don't need to target subscriptions-core independantly of Woo Subscriptions, I'd suggest just targeting the Woo Subscriptions version.

mattallan commented 6 months ago

Hey @peterwilsoncc, yes this change will go out in subcriptions-core 6.9.0 which is planned to be released in Woo Subscriptions 6.1 this week (28th March). Apologies for the confusion with our versioning šŸ˜…

My recommendation would be to forget about the version of Subscriptions Core as we're trying to move back to just the one subscription plugin, so to check the version that these changes are in I would use either approach:

class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '6.1.0', '>=' );
version_compare( WC_Subscriptions_Core_Plugin::instance()->get_plugin_version(), '6.1.0', '>=' )
mattallan commented 6 months ago

Oh @james-allan just responded 1 minute before me šŸ˜„ Good thing we're on the same page!!