Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
81 stars 29 forks source link

Update trash_users_subscriptions() to properly delete subscriptions in HPOS environments. #520

Closed mattallan closed 9 months ago

mattallan commented 9 months ago

Description

When a user/subscriber is deleted, we hook onto the delete_user hook and delete all subscriptions that belong to this user. This function was still using wp_post_delete() which doesn't work in a HPOS environment resulting in subscriptions remaining without a customer attached.

This PR updates the trash_users_subscriptions() function and replaces the wp_post_delete( $id ) method with $subscription->delete( true ) so work in both HPOS & WP Posts environments.

How to test this PR

  1. Enable High-performance order storage on your store (found: WC > Settings > Advanced > Features)
  2. Create a throw-away user by going to WP Admin > Users > Add New
  3. Create the user with the minimum details i.e. customer1 customer1@example.com and default password
  4. Go to WC > Subscriptions > Add New
  5. Create a basic subscription for this new customer and use the actions drop-down to create a pending parent order.
  6. Go to WP Admin > Users and delete the user.
  7. On trunk you will notice the subscription still exists without a customer and the parent order will have a note saying the subscription was deleted.
  8. On this branch the subscription will be removed.

Product impact