-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(esp-sync): get inactive subscriptions with failed or pending orders #3658
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works.
I have pushed one commit. I noticed that if a subscription is set to on-hold, the Membership status would not get the "Ex-" prefix.
I have a small performance feedback:
While looping through the related orders, if we find one completed
order, that would be enough. In the current implementation we'll always loop through all the orders, and call wc_get_order
to all of them even if it's not necessary. It would be better to exit the loop when we find the first completed order.
And a nitpick. You don't need array_keys
there, as get_related_orders
also return the IDs in the values. example:
array(1) {
[4788]=>
int(4788)
}
@leogermani 82f889a does a little refactoring to address your feedback—thanks! |
// Only subscriptions that had a completed order are considered. | ||
if ( ! empty( $subscription->get_date( 'last_order_date_completed' ) ) ) { | ||
// Only subscriptions that have at least one completed order are considered. | ||
$related_orders = $subscription->get_related_orders( 'all' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the change to add the all
param is making woocommerce-subscriptions call wc_get_order for every order. So the gain in performance we had here was nulled by the fact that we are calling wc_get_order for all orders anyways. (see https://github.com/woocommerce/-unused-woocommerce-subscriptions/blob/bff84d85400f964c4ddfc048381db9bf7be400d7/includes/class-wc-subscription.php#L1836 )
We can just get the ids as before, and only fetch the order details as needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But now that we need to check the status of each order, we'd need to call wc_get_order()
ourselves anyway if we don't use the all
argument. Unless there's a way to get an order's status by order ID without fetching the whole object that I don't know about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I think I found a better way to do it. 882a2e6 uses WC_Subscription::get_payment_count()
to check the number of completed orders for the subscription instead of fetching and iterating through all orders.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b921675 should be safest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@leogermani let me know if you have any other improvements to suggest!
## [5.10.6](v5.10.5...v5.10.6) (2025-01-09) ### Bug Fixes * **esp-sync:** get inactive subscriptions with failed or pending orders ([#3658](#3658)) ([cca29d3](cca29d3))
🎉 This PR is included in version 5.10.6 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
All Submissions:
Changes proposed in this Pull Request:
Fixes an issue where
Newspack\Reader_Activation\Sync\WooCommerce::get_most_recent_cancelled_or_expired_subscription( $user_id )
fails to get subscriptions whose most recent order has afailed
orpending
status. This is because$subscription->get_date( 'last_order_date_completed' )
will only return a value if the subscription's most recent order has acompleted
status.How to test the changes in this Pull Request:
release
, usingwp shell
, run\Newspack\Reader_Activation\Sync\WooCommerce::get_contact_from_customer( $user_id );
to get the contact data for the order. Observe that the contact data does not contain any info about the on-hold subscription:wp shell
session, and confirm that\Newspack\Reader_Activation\Sync\WooCommerce::get_contact_from_customer( $user_id );
now contains info about the on-hold subscription:Other information: