@@ -61,6 +61,27 @@ function wcs_order_contains_renewal( $order ) {
61
61
return apply_filters ( 'woocommerce_subscriptions_is_renewal_order ' , $ is_renewal , $ order );
62
62
}
63
63
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
+
64
85
/**
65
86
* Checks the cart to see if it contains a subscription product renewal.
66
87
*
@@ -127,10 +148,7 @@ function wcs_get_subscriptions_for_renewal_order( $order ) {
127
148
*/
128
149
function wcs_get_last_non_early_renewal_order ( $ subscription ) {
129
150
$ 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 );
134
152
135
153
foreach ( $ renewal_orders as $ renewal_order ) {
136
154
if ( ! wcs_order_contains_early_renewal ( $ renewal_order ) ) {
@@ -142,6 +160,30 @@ function wcs_get_last_non_early_renewal_order( $subscription ) {
142
160
return $ last_non_early_renewal ;
143
161
}
144
162
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
+
145
187
/**
146
188
* Checks if manual renewals are required - automatic renewals are disabled.
147
189
*
0 commit comments