Skip to content
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

Merged
merged 7 commits into from
Jan 9, 2025
14 changes: 11 additions & 3 deletions includes/reader-activation/sync/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,16 @@ function( $acc, $subscription_id ) {
$subscription = \wcs_get_subscription( $subscription_id );
if ( $subscription->has_status( WooCommerce_Connection::FORMER_SUBSCRIBER_STATUSES ) ) {

// 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' );
Copy link
Contributor

@leogermani leogermani Jan 9, 2025

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

Copy link
Contributor Author

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b921675 should be safest

Copy link
Contributor Author

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!

$completed_order = false;
foreach ( $related_orders as $order_id => $order ) {
if ( $order->has_status( 'completed' ) ) {
$completed_order = $order;
break;
}
}
if ( ! empty( $completed_order ) ) {
$acc[] = $subscription_id;
}
}
Expand Down Expand Up @@ -257,7 +265,7 @@ private static function get_order_metadata( $order, $payment_page_url = false )
}

// If the subscription has moved to a cancelled or expired status.
if ( $current_subscription->has_status( [ 'cancelled', 'expired' ] ) ) {
if ( $current_subscription->has_status( [ 'cancelled', 'expired', 'on-hold' ] ) ) {
$donor_status = 'Ex-' . $donor_status;
}
$metadata['membership_status'] = $donor_status;
Expand Down
Loading