Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit f474a09

Browse files
committed
Fix renewal orders wrong status update
1 parent 5ba92ad commit f474a09

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

includes/class-wc-subscriptions-renewal-order.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ public static function maybe_record_subscription_payment( $order_id, $orders_old
8989
return;
9090
}
9191

92+
// Check if this is the last renewal order for a subscription
93+
if ( ! wcs_order_is_latest_renewal( $order_id ) ) {
94+
return;
95+
}
96+
9297
$subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id );
9398
$was_activated = false;
9499
$order = wc_get_order( $order_id );

includes/wcs-renewal-functions.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ function wcs_order_contains_renewal( $order ) {
6161
return apply_filters( 'woocommerce_subscriptions_is_renewal_order', $is_renewal, $order );
6262
}
6363

64+
/**
65+
* @param $order WC_Order The order object.
66+
* @return bool|void Weather the order is the latest renewal order.
67+
*/
68+
function wcs_order_is_latest_renewal( $order ) {
69+
$subscriptions = wcs_get_subscriptions_for_renewal_order( $order );
70+
71+
if ( empty( $subscriptions ) ) {
72+
return false;
73+
}
74+
75+
foreach ( $subscriptions as $subscription ) {
76+
$last_renewal_order = wcs_get_last_renewal_order( $subscription );
77+
if ( $last_renewal_order && $last_renewal_order->get_id() === $order->get_id() ) {
78+
return true;
79+
}
80+
}
81+
82+
return false;
83+
}
84+
6485
/**
6586
* Checks the cart to see if it contains a subscription product renewal.
6687
*
@@ -127,10 +148,7 @@ function wcs_get_subscriptions_for_renewal_order( $order ) {
127148
*/
128149
function wcs_get_last_non_early_renewal_order( $subscription ) {
129150
$last_non_early_renewal = false;
130-
$renewal_orders = $subscription->get_related_orders( 'all', 'renewal' );
131-
132-
// We need the orders sorted by the date they were created, with the newest first.
133-
wcs_sort_objects( $renewal_orders, 'date_created', 'descending' );
151+
$renewal_orders = wcs_get_newest_renewal_orders( $subscription );
134152

135153
foreach ( $renewal_orders as $renewal_order ) {
136154
if ( ! wcs_order_contains_early_renewal( $renewal_order ) ) {
@@ -142,6 +160,30 @@ function wcs_get_last_non_early_renewal_order( $subscription ) {
142160
return $last_non_early_renewal;
143161
}
144162

163+
/**
164+
* Get the last renewal order (early renewals included).
165+
*
166+
* @param WC_Subscription $subscription The subscription object.
167+
* @return WC_Order|bool The last non-early renewal order, otherwise false.
168+
*/
169+
function wcs_get_last_renewal_order( $subscription ) {
170+
$renewal_orders = wcs_get_newest_renewal_orders( $subscription );
171+
return $renewal_orders ? reset( $renewal_orders ) : false;
172+
}
173+
174+
/**
175+
* @param $subscription WC_Subscription The subscription object.
176+
* @return array The latest renewal orders
177+
*/
178+
function wcs_get_newest_renewal_orders( $subscription ) {
179+
$renewal_orders = $subscription->get_related_orders( 'all', 'renewal' );
180+
181+
// We need the orders sorted by the date they were created, with the newest first.
182+
wcs_sort_objects( $renewal_orders, 'date_created', 'descending' );
183+
184+
return $renewal_orders;
185+
}
186+
145187
/**
146188
* Checks if manual renewals are required - automatic renewals are disabled.
147189
*

0 commit comments

Comments
 (0)