PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.1.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.1.0
2.4.0 2.3.0 2.2.0 2.1.0 2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 All 65 releases
← All changes | includes/Illuminate/Order.php +115 -94 1.10.1 → 2.1.0 View file →
@@ -71,9 +71,9 @@
71 71 delete_post_meta( $subscription_id, '_subscrpt_trial_started' );
72 72 delete_post_meta( $subscription_id, '_subscrpt_trial_ended' );
73 73 }
74 74
75 - $next_date = sdevs_wp_strtotime( $recurr_timing, time() );
75 + $next_date = $this->get_anchored_next_date( $subscription_id, $recurr_timing, (int) $subscription_history->order_id );
76 76
77 77 } elseif ( 'early-renew' === $subscription_history->type ) {
78 78 if ( $trial ) {
79 79 delete_post_meta( $subscription_id, '_subscrpt_trial' );
@@ -84,15 +84,99 @@
84 84
85 85 $next_date = sdevs_wp_strtotime( $recurr_timing, time() );
86 86 }
87 87
88 - // Allow filtering of next due date logic
89 - $next_date = apply_filters( 'subscrpt_split_payment_next_due_date', $next_date, $subscription_id, $recurr_timing, $subscription_history->type );
88 + // Split payment: no next date after the final installment.
89 + if (
90 + in_array( $subscription_history->type, array( 'renew', 'early-renew' ), true )
91 + && function_exists( 'subscrpt_is_max_payments_reached' )
92 + && subscrpt_is_max_payments_reached( $subscription_id )
93 + ) {
94 + return;
95 + }
90 96
97 + /**
98 + * Filter the subscription next payment date before it is saved.
99 + *
100 + * General-purpose hook (not scoped to split payment) for adjusting the
101 + * computed next renewal date. Note `$next_date` may be null for history
102 + * types other than 'new'/'renew'/'early-renew' (e.g. switch orders).
103 + *
104 + * The deprecated `subscrpt_split_payment_next_due_date` filter is bridged
105 + * onto this hook in LegacyCompat.php for backward compatibility.
106 + *
107 + * @param int|null $next_date Computed next payment timestamp, or null.
108 + * @param int $subscription_id Subscription ID.
109 + * @param string $recurr_timing Recurring timing string (e.g. "1 month").
110 + * @param string $type Subscription history type.
111 + */
112 + $next_date = apply_filters( 'subscrpt_subscription_next_date', $next_date, $subscription_id, $recurr_timing, $subscription_history->type );
113 +
91 114 update_post_meta( $subscription_id, '_subscrpt_next_date', $next_date );
92 115 }
93 116
94 117 /**
118 + * Calculate a renewal's next payment date, anchored to the previous due date.
119 + *
120 + * Computing from time() instead makes every cycle inherit however late the
121 + * renewal was actually processed — with an hourly cron a due date of 02:00:05
122 + * is missed by the 02:00:03 run and lands at 03:00, and that hour is carried
123 + * into every following cycle until a whole billing period is skipped. Anchoring
124 + * to the stored due date keeps the billing time-of-day stable instead.
125 + *
126 + * When payment arrives more than one period late (manual renewal, cron outage),
127 + * the anchor is stepped forward period by period so the returned date is always
128 + * in the future — otherwise the subscription would be due again immediately.
129 + *
130 + * Because this runs on every activating status transition of the same renewal
131 + * order (pending → processing → completed), the order that last moved the date
132 + * is recorded so repeat transitions do not advance the cycle again.
133 + *
134 + * @param int $subscription_id Subscription ID.
135 + * @param string $recurr_timing Recurring timing string (e.g. "1 month").
136 + * @param int $renewal_order_id Renewal order driving this activation.
137 + *
138 + * @return int Next payment timestamp.
139 + */
140 + private function get_anchored_next_date( $subscription_id, $recurr_timing, $renewal_order_id = 0 ) {
141 + $now = time();
142 + $anchor = (int) get_post_meta( $subscription_id, '_subscrpt_next_date', true );
143 +
144 + if ( $anchor <= 0 ) {
145 + return sdevs_wp_strtotime( $recurr_timing, $now );
146 + }
147 +
148 + // This renewal order already advanced the date; keep it where it is.
149 + $dated_by = (int) get_post_meta( $subscription_id, '_subscrpt_next_date_set_by_order', true );
150 + if ( $renewal_order_id && $renewal_order_id === $dated_by ) {
151 + return $anchor;
152 + }
153 +
154 + if ( $renewal_order_id ) {
155 + update_post_meta( $subscription_id, '_subscrpt_next_date_set_by_order', $renewal_order_id );
156 + }
157 +
158 + $next_date = sdevs_wp_strtotime( $recurr_timing, $anchor );
159 +
160 + // Step forward until the date is in the future. Bail out if the timing string does not advance.
161 + $guard = 0;
162 + while ( $next_date <= $now && $guard < 1000 ) {
163 + $stepped = sdevs_wp_strtotime( $recurr_timing, $next_date );
164 + if ( $stepped <= $next_date ) {
165 + break;
166 + }
167 + $next_date = $stepped;
168 + ++$guard;
169 + }
170 +
171 + if ( $next_date <= $now ) {
172 + return sdevs_wp_strtotime( $recurr_timing, $now );
173 + }
174 +
175 + return $next_date;
176 + }
177 +
178 + /**
95 179 * Add custom column on order item.
96 180 *
97 181 * @return void
98 182 */
@@ -115,9 +199,8 @@
115 199 return;
116 200 }
117 201
118 202 $subtotal = '-';
119 - $item_id = $item->get_id();
120 203 $subscription_id = Helper::get_subscription_from_order_item_id( $item->get_id() );
121 204
122 205 if ( ! $subscription_id ) {
123 206 echo "<td class='item_recurring' width='15%'>-</td>";
@@ -124,10 +207,10 @@
124 207 return;
125 208 }
126 209 $subscription_id = $subscription_id->subscription_id;
127 210
128 - $price = get_post_meta( $subscription_id, '_subscrpt_price', true );
129 - $subtotal = Helper::format_price_with_order_item( $price, $item_id );
211 + // Strikes the original amount when a discount carries into renewals.
212 + $subtotal = Helper::get_subscription_recurring_price_html( $subscription_id, $item );
130 213 ?>
131 214 <td class="item_recurring" width="15%">
132 215 <div class="view">
133 216 <?php echo wp_kses_post( $subtotal ); ?>
@@ -189,12 +272,36 @@
189 272
190 273 foreach ( $histories as $history ) {
191 274 if ( 'new' === $history->type || 'renew' === $history->type ) {
192 275 $subscription_id = $history->subscription_id;
276 +
277 + // Renewals ignore the intermediate `processing` state for renewal orders.
278 + if ( 'renew' === $history->type && 'processing' === $order->get_status() ) {
279 + continue;
280 + }
281 +
282 + // Capture the status before the max-payments check below may flip it.
283 + $current_status = get_post_status( $subscription_id );
284 +
285 + // Split payment: complete instead of re-activate after the final installment.
286 + $target_status = $post_status;
287 + if (
288 + 'active' === $target_status
289 + && function_exists( 'subscrpt_is_max_payments_reached' )
290 + && subscrpt_is_max_payments_reached( $subscription_id )
291 + ) {
292 + $target_status = 'completed';
293 + }
294 +
295 + // Skip no-op transitions so the note and side effects don't duplicate.
296 + if ( $current_status === $target_status ) {
297 + continue;
298 + }
299 +
193 300 wp_update_post(
194 301 array(
195 302 'ID' => $subscription_id,
196 - 'post_status' => $post_status,
303 + 'post_status' => $target_status,
197 304 )
198 305 );
199 306
200 307 // If possible change order status to completed if it has a trial subscription.
@@ -209,9 +316,9 @@
209 316
210 317 // Add enhanced split payment activity logging
211 318 $this->add_split_payment_activity_note( $history->subscription_id, $history->type, $post_status, $order );
212 319
213 - Action::write_comment( $post_status, $history->subscription_id );
320 + Action::write_comment( $target_status, $history->subscription_id );
214 321 } else {
215 322 do_action( 'subscrpt_order_status_changed', $order, $history );
216 323 }
217 324 }
@@ -322,94 +429,8 @@
322 429 $max_payments,
323 430 $subscription_id
324 431 );
325 432 $order->add_order_note( $order_note );
326 - }
327 -
328 - // Add payment milestone notes for significant progress
329 - $this->add_payment_milestone_notes( $subscription_id, $payments_made, $max_payments );
330 - }
331 -
332 - /**
333 - * Add payment milestone notes for significant progress (25%, 50%, 75%, 100%).
334 - *
335 - * @param int $subscription_id Subscription ID.
336 - * @param int $payments_made Number of payments made.
337 - * @param int $max_payments Maximum number of payments.
338 - */
339 - private function add_payment_milestone_notes( $subscription_id, $payments_made, $max_payments ) {
340 - if ( ! $max_payments || $max_payments <= 0 ) {
341 - return;
342 - }
343 -
344 - $percentage = round( ( $payments_made / $max_payments ) * 100 );
345 - $milestone_key = '_subscrpt_milestone_' . $percentage . '_logged';
346 -
347 - // Check if this milestone has already been logged
348 - if ( get_post_meta( $subscription_id, $milestone_key, true ) ) {
349 - return;
350 - }
351 -
352 - $milestone_note = '';
353 - $activity_type = '';
354 -
355 - switch ( $percentage ) {
356 - case 25:
357 - $milestone_note = sprintf(
358 - /* translators: %1$d: payments made, %2$d: total payments */
359 - __( 'Payment milestone reached: %1$d of %2$d payments completed (25%%).', 'subscription' ),
360 - $payments_made,
361 - $max_payments
362 - );
363 - $activity_type = __( 'Payment Milestone - 25%', 'subscription' );
364 - break;
365 -
366 - case 50:
367 - $milestone_note = sprintf(
368 - /* translators: %1$d: payments made, %2$d: total payments */
369 - __( 'Payment milestone reached: %1$d of %2$d payments completed (50%%). Halfway there!', 'subscription' ),
370 - $payments_made,
371 - $max_payments
372 - );
373 - $activity_type = __( 'Payment Milestone - 50%', 'subscription' );
374 - break;
375 -
376 - case 75:
377 - $milestone_note = sprintf(
378 - /* translators: %1$d: payments made, %2$d: total payments */
379 - __( 'Payment milestone reached: %1$d of %2$d payments completed (75%%). Almost complete!', 'subscription' ),
380 - $payments_made,
381 - $max_payments
382 - );
383 - $activity_type = __( 'Payment Milestone - 75%', 'subscription' );
384 - break;
385 -
386 - case 100:
387 - $milestone_note = sprintf(
388 - /* translators: %1$d: payments made, %2$d: total payments */
389 - __( 'Payment milestone reached: %1$d of %2$d payments completed (100%%). Split payment plan complete!', 'subscription' ),
390 - $payments_made,
391 - $max_payments
392 - );
393 - $activity_type = __( 'Payment Milestone - 100%', 'subscription' );
394 - break;
395 - }
396 -
397 - // Add milestone note if applicable
398 - if ( $milestone_note ) {
399 - $comment_id = wp_insert_comment(
400 - array(
401 - 'comment_author' => 'Subscription for WooCommerce',
402 - 'comment_content' => $milestone_note,
403 - 'comment_post_ID' => $subscription_id,
404 - 'comment_type' => 'order_note',
405 - )
406 - );
407 - update_comment_meta( $comment_id, '_subscrpt_activity', $activity_type );
408 - update_comment_meta( $comment_id, '_subscrpt_activity_type', 'split_payment' );
409 -
410 - // Mark milestone as logged
411 - update_post_meta( $subscription_id, $milestone_key, current_time( 'timestamp' ) );
412 433 }
413 434 }
414 435
415 436 /**