| @@ -81,11 +81,21 @@ | ||
| 81 | 81 | delete_post_meta( $subscription_id, '_subscrpt_trial_started' ); |
| 82 | 82 | delete_post_meta( $subscription_id, '_subscrpt_trial_ended' ); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - $next_date = sdevs_wp_strtotime( $recurr_timing, time() ); | |
| 85 | + // Extend from the current due date so the days already paid for are kept. | |
| 86 | + $next_date = $this->get_anchored_next_date( $subscription_id, $recurr_timing, (int) $subscription_history->order_id ); | |
| 86 | 87 | } |
| 87 | 88 | |
| 89 | + // Split payment: no next date after the final installment. | |
| 90 | + if ( | |
| 91 | + in_array( $subscription_history->type, array( 'renew', 'early-renew' ), true ) | |
| 92 | + && function_exists( 'subscrpt_is_max_payments_reached' ) | |
| 93 | + && subscrpt_is_max_payments_reached( $subscription_id ) | |
| 94 | + ) { | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + | |
| 88 | 98 | /** |
| 89 | 99 | * Filter the subscription next payment date before it is saved. |
| 90 | 100 | * |
| 91 | 101 | * General-purpose hook (not scoped to split payment) for adjusting the |
| @@ -263,12 +273,36 @@ | ||
| 263 | 273 | |
| 264 | 274 | foreach ( $histories as $history ) { |
| 265 | 275 | if ( 'new' === $history->type || 'renew' === $history->type ) { |
| 266 | 276 | $subscription_id = $history->subscription_id; |
| 277 | + | |
| 278 | + // Renewals ignore the intermediate `processing` state for renewal orders. | |
| 279 | + if ( 'renew' === $history->type && 'processing' === $order->get_status() ) { | |
| 280 | + continue; | |
| 281 | + } | |
| 282 | + | |
| 283 | + // Capture the status before the max-payments check below may flip it. | |
| 284 | + $current_status = get_post_status( $subscription_id ); | |
| 285 | + | |
| 286 | + // Split payment: complete instead of re-activate after the final installment. | |
| 287 | + $target_status = $post_status; | |
| 288 | + if ( | |
| 289 | + 'active' === $target_status | |
| 290 | + && function_exists( 'subscrpt_is_max_payments_reached' ) | |
| 291 | + && subscrpt_is_max_payments_reached( $subscription_id ) | |
| 292 | + ) { | |
| 293 | + $target_status = 'completed'; | |
| 294 | + } | |
| 295 | + | |
| 296 | + // Skip no-op transitions so the note and side effects don't duplicate. | |
| 297 | + if ( $current_status === $target_status ) { | |
| 298 | + continue; | |
| 299 | + } | |
| 300 | + | |
| 267 | 301 | wp_update_post( |
| 268 | 302 | array( |
| 269 | 303 | 'ID' => $subscription_id, |
| 270 | - 'post_status' => $post_status, | |
| 304 | + 'post_status' => $target_status, | |
| 271 | 305 | ) |
| 272 | 306 | ); |
| 273 | 307 | |
| 274 | 308 | // If possible change order status to completed if it has a trial subscription. |
| @@ -283,9 +317,9 @@ | ||
| 283 | 317 | |
| 284 | 318 | // Add enhanced split payment activity logging |
| 285 | 319 | $this->add_split_payment_activity_note( $history->subscription_id, $history->type, $post_status, $order ); |
| 286 | 320 | |
| 287 | - Action::write_comment( $post_status, $history->subscription_id ); | |
| 321 | + Action::write_comment( $target_status, $history->subscription_id ); | |
| 288 | 322 | } else { |
| 289 | 323 | do_action( 'subscrpt_order_status_changed', $order, $history ); |
| 290 | 324 | } |
| 291 | 325 | } |
| @@ -396,94 +430,8 @@ | ||
| 396 | 430 | $max_payments, |
| 397 | 431 | $subscription_id |
| 398 | 432 | ); |
| 399 | 433 | $order->add_order_note( $order_note ); |
| 400 | - } | |
| 401 | - | |
| 402 | - // Add payment milestone notes for significant progress | |
| 403 | - $this->add_payment_milestone_notes( $subscription_id, $payments_made, $max_payments ); | |
| 404 | - } | |
| 405 | - | |
| 406 | - /** | |
| 407 | - * Add payment milestone notes for significant progress (25%, 50%, 75%, 100%). | |
| 408 | - * | |
| 409 | - * @param int $subscription_id Subscription ID. | |
| 410 | - * @param int $payments_made Number of payments made. | |
| 411 | - * @param int $max_payments Maximum number of payments. | |
| 412 | - */ | |
| 413 | - private function add_payment_milestone_notes( $subscription_id, $payments_made, $max_payments ) { | |
| 414 | - if ( ! $max_payments || $max_payments <= 0 ) { | |
| 415 | - return; | |
| 416 | - } | |
| 417 | - | |
| 418 | - $percentage = round( ( $payments_made / $max_payments ) * 100 ); | |
| 419 | - $milestone_key = '_subscrpt_milestone_' . $percentage . '_logged'; | |
| 420 | - | |
| 421 | - // Check if this milestone has already been logged | |
| 422 | - if ( get_post_meta( $subscription_id, $milestone_key, true ) ) { | |
| 423 | - return; | |
| 424 | - } | |
| 425 | - | |
| 426 | - $milestone_note = ''; | |
| 427 | - $activity_type = ''; | |
| 428 | - | |
| 429 | - switch ( $percentage ) { | |
| 430 | - case 25: | |
| 431 | - $milestone_note = sprintf( | |
| 432 | - /* translators: %1$d: payments made, %2$d: total payments */ | |
| 433 | - __( 'Payment milestone reached: %1$d of %2$d payments completed (25%%).', 'subscription' ), | |
| 434 | - $payments_made, | |
| 435 | - $max_payments | |
| 436 | - ); | |
| 437 | - $activity_type = __( 'Payment Milestone - 25%', 'subscription' ); | |
| 438 | - break; | |
| 439 | - | |
| 440 | - case 50: | |
| 441 | - $milestone_note = sprintf( | |
| 442 | - /* translators: %1$d: payments made, %2$d: total payments */ | |
| 443 | - __( 'Payment milestone reached: %1$d of %2$d payments completed (50%%). Halfway there!', 'subscription' ), | |
| 444 | - $payments_made, | |
| 445 | - $max_payments | |
| 446 | - ); | |
| 447 | - $activity_type = __( 'Payment Milestone - 50%', 'subscription' ); | |
| 448 | - break; | |
| 449 | - | |
| 450 | - case 75: | |
| 451 | - $milestone_note = sprintf( | |
| 452 | - /* translators: %1$d: payments made, %2$d: total payments */ | |
| 453 | - __( 'Payment milestone reached: %1$d of %2$d payments completed (75%%). Almost complete!', 'subscription' ), | |
| 454 | - $payments_made, | |
| 455 | - $max_payments | |
| 456 | - ); | |
| 457 | - $activity_type = __( 'Payment Milestone - 75%', 'subscription' ); | |
| 458 | - break; | |
| 459 | - | |
| 460 | - case 100: | |
| 461 | - $milestone_note = sprintf( | |
| 462 | - /* translators: %1$d: payments made, %2$d: total payments */ | |
| 463 | - __( 'Payment milestone reached: %1$d of %2$d payments completed (100%%). Split payment plan complete!', 'subscription' ), | |
| 464 | - $payments_made, | |
| 465 | - $max_payments | |
| 466 | - ); | |
| 467 | - $activity_type = __( 'Payment Milestone - 100%', 'subscription' ); | |
| 468 | - break; | |
| 469 | - } | |
| 470 | - | |
| 471 | - // Add milestone note if applicable | |
| 472 | - if ( $milestone_note ) { | |
| 473 | - $comment_id = wp_insert_comment( | |
| 474 | - array( | |
| 475 | - 'comment_author' => 'Subscription for WooCommerce', | |
| 476 | - 'comment_content' => $milestone_note, | |
| 477 | - 'comment_post_ID' => $subscription_id, | |
| 478 | - 'comment_type' => 'order_note', | |
| 479 | - ) | |
| 480 | - ); | |
| 481 | - update_comment_meta( $comment_id, '_subscrpt_activity', $activity_type ); | |
| 482 | - update_comment_meta( $comment_id, '_subscrpt_activity_type', 'split_payment' ); | |
| 483 | - | |
| 484 | - // Mark milestone as logged | |
| 485 | - update_post_meta( $subscription_id, $milestone_key, current_time( 'timestamp' ) ); | |
| 486 | 434 | } |
| 487 | 435 | } |
| 488 | 436 | |
| 489 | 437 | /** |