Fixes #221
Fixes #195
Description
This PR updates all of the WP Post-related hooks we were using to hook onto the trashing, deleting and untrashing of orders and subscriptions in the WC_Subscriptions_Manager
class to now use the CRUD/HPOS equivalent hooks so that this functionality continues to work on stores that are using custom order tables.
This PR consists of the following changes:
- Moved all of the trash, untrash & delete hooks into a new function that is ran on
woocommerce_loaded
- Added new
add_action()
's calls for when HPOS is enabled to hook onto the new WC order hooks
- Updated the existing
maybe_trash_subscription()
& maybe_cancel_subscription()
functions to use CRUD methods
1. Moved actions into a new function on woocommerce_loaded
In order to load the new hooks we need to run wcs_is_custom_order_tables_usage_enabled()
but this function only works after WC has been loaded.
While testing this PR I was getting errors due to some WC classes/instances not existing yet when trying to check if HPOS is enabled.
To fix this, I moved these hooks into their own function which is attached to the WC loaded do action.
2. New Add Actions
Here are all of the _post
hooks changing in this PR and their new HPOS equivalent hooks:
wp_trash_post
is now woocommerce_before_trash_order
untrashed_post
is now woocommerce_untrash_order
before_delete_post
is now woocommerce_before_delete_order
trashed_post
is now woocommerce_trash_order
deleted_post is now
woocommerce_delete_order`
To avoid breaking backwards compatibility, I made it so these new action hooks are only used on stores with HPOS enabled.
3. Updating functions to use CRUD methods
Most of the changes in these functions are pretty self-explanatory but there are some I wanted to make note of.
order_data_store::untrash_order()
method only exists in the OrderTablesDataStore
class so I made it so we only use this function if it exists, otherwise, fall back to using the wp_untrash_post()
function.
When fixing fix_trash_meta_status()
to work in CRUD, I couldn't use update_meta_data()
as it was creating duplicate rows in the database, so I had to use the read_meta()
and update_meta()
approach which writes this data directly to the DB and works in both HPOS and none-HPOS environments.
How to test this PR
There are 4 main flows that need to be tested in this PR:
- Trashing a parent order, cancels the subscription first, then trashes it
- Trashing a parent order correctly sets the
_wp_trash_meta_status
to wc-cancelled
if the subscription previously had an active status.
- Untrashing a parent order also untrashes the subscription (Note: untrashed subscriptions go to pending status)
- Deleting a parent order deletes the subscription
My own testing on this PR:
HPOS off:
- [x] Trash a parent order
- [x] Confirm subscription was cancelled first (need to check order notes)
- [x] Confirm subscription is trashed
- [x] Confirm
_wp_trash_meta_status
is set on subscription
- [x] Untrash parent order
- [x] Confirm subscription is also untrashed
- [x] Delete a parent order
- [x] Confirm subscription has been deleted from DB
- [x] Confirm
woocommerce_subscription_trashed
action hook is triggered after trash
- [x] Confirm
woocommerce_subscription_deleted
action hook is triggered after delete
HPOS on, syncing off:
- [x] Trash a parent order
- [x] Confirm subscription was cancelled first (need to check order notes)
- [x] Confirm subscription is trashed
- [x] Confirm
_wp_trash_meta_status
is set on subscription
- [x] Untrash parent order
- [x] Confirm subscription is also untrashed
- [x] Delete a parent order
- [x] Confirm subscription has been deleted from DB
- [x] Confirm
woocommerce_subscription_trashed
action hook is triggered after trash
- [x] Confirm
woocommerce_subscription_deleted
action hook is triggered after delete
HPOS on, syncing on:
- [x] Trash a parent order
- [x] Confirm subscription was cancelled first (need to check order notes)
- [x] Confirm subscription is trashed
- [x] Confirm
_wp_trash_meta_status
is set on subscription
- [x] Untrash parent order
- [x] Confirm subscription is also untrashed
- [x] Delete a parent order
- [x] Confirm subscription has been deleted from DB
- [x] Confirm
woocommerce_subscription_trashed
action hook is triggered after trash
- [x] Confirm
woocommerce_subscription_deleted
action hook is triggered after delete
WC 6.8 (HPOS off):
- [x] Trash a parent order
- [x] Confirm subscription was cancelled first (need to check order notes)
- [x] Confirm subscription is trashed
- [x] Confirm
_wp_trash_meta_status
is set on subscription
- [x] Untrash parent order
- [x] Confirm subscription is also untrashed
- [x] Delete a parent order
- [x] Confirm subscription has been deleted from DB
- [x] Confirm
woocommerce_subscription_trashed
action hook is triggered after trash
- [x] Confirm
woocommerce_subscription_deleted
action hook is triggered after delete
Product impact
- [x] Added changelog entry (or does not apply)
- [x] Will this PR affect WooCommerce Subscriptions? yes/no/tbc, add issue ref
- [x] Will this PR affect WooCommerce Payments? yes/no/tbc, add issue ref
- [x] Added deprecated functions, hooks or classes to the spreadsheet
pr: needs review compatibility: HPOS