Closed mattallan closed 7 months ago
I've run into a bit of a strange issue while testing this PR.
When the post record is backported, the related order cache meta keys and values are being duplicated.
For example:
What is causing that is an issue in WooCommerce Core. I've submitted a PR to WC here: https://github.com/woocommerce/woocommerce/pull/41281
In short WC core's backporting functions are failing to locate the existing related order cache meta because it's comparing a serialised version of the array to the array a:0:{} !=== array()
here it doesn't match and so they call add_post_meta()
adding a new row. That PR will fix it for us too.
I need to look into whether this PR is contributing to that and if this duplicate meta will cause any issues.
I need to look into whether this PR is contributing to that and if this duplicate meta will cause any issues.
It was getting difficult to keep straight in my head all the different testing versions, scenarios and results so I've included a table to illustrate the issues.
WC Core | Subscriptions Core | Result |
---|---|---|
NO RELATED ORDERS | ||
trunk |
develop |
🐛🐛 0 dates and no related order cache meta |
https://github.com/woocommerce/woocommerce/pull/41281 | develop |
🐛 👍 0 dates but good related order cache meta |
trunk |
This PR | 👍 🐛 Good dates, dupped related order caches |
https://github.com/woocommerce/woocommerce/pull/41281 | This PR | 👍👍 Dates and related order caches are good |
RELATED ORDERS | ||
trunk |
develop |
🐛🐛 0 dates and duplicate related order caches |
https://github.com/woocommerce/woocommerce/pull/41281 | develop |
🐛 👍 0 dates but good related order cache meta |
trunk |
This PR | 👍 🐛 Good dates, dupped related order caches |
https://github.com/woocommerce/woocommerce/pull/41281 | This PR | 👍👍 Dates and related order caches are good |
With this table of results in mind, we have 3 options.
add_{$meta_type}_metadata
, and return false
if that meta already exists or have a cleanup function that runs after backfill has happened to make sure it deletes the duplicates. We could use your existing set_renewal_order_ids_cache()
setters to run this. I noticed the unit tests were failing because of the version of yoast/phpunit-polyfills
we were using. I've just bumped this from 1.0.3
to 1.1.0
in b487239 and it's looking like it has fixed the issues.
I think we can merge this though despite the issues raised in https://github.com/Automattic/woocommerce-subscriptions-core/pull/538#issuecomment-1801122335. I'm working on some changes separately that I'll submit a PR for today
Thanks @james-allan !! Once the checks pass I'll go ahead and merge this.
Fixes https://github.com/woocommerce/woocommerce-subscriptions/issues/4572
Description
With custom order tables (HPOS) and compatibility mode (data syncing) enabled, some subscription metadata is not correctly backfilled to the postmeta table, namely all of our
_schedule_{date_type}
meta and our_subscription_{order_type}_order_ids_cache
meta. Here's a full list:_schedule_start
_schedule_trial_end
_schedule_next_payment
_schedule_cancelled
_schedule_end
_schedule_payment_retry
_subscription_renewal_order_ids_cache
_subscription_resubscribe_order_ids_cache
_subscription_switch_order_ids_cache
Here's a screenshot from
trunk
of the subscriptions meta in the WC orders meta and WP posts meta tables after a subscription is synced. Notice how the dates are set to 0 and notice how the cache meta is completely missing:wp_wc_orders_meta
wp_postmeta
Why is this meta not backfilled?
During the backfill process, the
OrdersTableDataStore::backfill_post_record()
function calls$post_order->set_props( $order->get_data() );
which gets all of the data stored on the order object and tries to call the setter methods for each prop. Because we don't have setters for our dates and cache metadata this data is not set.There's a bit more to it related to
Abstract_WC_Order_Data_Store_CPT->update_order_meta_from_object()
andget_internal_data_store_key_getters()
but that's the crux of itHow to test this PR
wc_schedule_pending_batch_processes
pending scheduled actionwc_run_batch_process
scheduled actionwp_postmeta
for the subscription IDwc_orders_meta
towp_postmeta
Product impact