| @@ -16,8 +16,19 @@ | ||
| 16 | 16 | * @param int $subscription_id Subscription ID. |
| 17 | 17 | * @param bool $write_comment Write comment?. |
| 18 | 18 | */ |
| 19 | 19 | public static function status( string $status, int $subscription_id, bool $write_comment = true ) { |
| 20 | + // A split-payment subscription whose final installment is paid is terminal: | |
| 21 | + // never (re)activate it. Any renewal-payment path that tries to set it active | |
| 22 | + // after completion is coerced to `completed` so the status can't flip back. | |
| 23 | + if ( | |
| 24 | + 'active' === $status | |
| 25 | + && function_exists( 'subscrpt_is_max_payments_reached' ) | |
| 26 | + && subscrpt_is_max_payments_reached( $subscription_id ) | |
| 27 | + ) { | |
| 28 | + $status = 'completed'; | |
| 29 | + } | |
| 30 | + | |
| 20 | 31 | $old_status = get_post_status( $subscription_id ); |
| 21 | 32 | |
| 22 | 33 | wp_update_post( |
| 23 | 34 | array( |
| @@ -25,9 +36,10 @@ | ||
| 25 | 36 | 'post_status' => $status, |
| 26 | 37 | ) |
| 27 | 38 | ); |
| 28 | 39 | |
| 29 | - if ( $write_comment ) { | |
| 40 | + // Only note a real transition — never re-log the same status. | |
| 41 | + if ( $write_comment && $old_status !== $status ) { | |
| 30 | 42 | self::write_comment( $status, $subscription_id ); |
| 31 | 43 | } |
| 32 | 44 | |
| 33 | 45 | // Trigger status change action |
| @@ -64,12 +76,35 @@ | ||
| 64 | 76 | break; |
| 65 | 77 | case 'on-hold': |
| 66 | 78 | self::on_hold( $subscription_id ); |
| 67 | 79 | break; |
| 80 | + case 'completed': | |
| 81 | + self::completed( $subscription_id ); | |
| 82 | + break; | |
| 68 | 83 | } |
| 69 | 84 | } |
| 70 | 85 | |
| 71 | 86 | /** |
| 87 | + * Write Comment About Completed Subscription. | |
| 88 | + * | |
| 89 | + * @param int $subscription_id Subscription ID. | |
| 90 | + */ | |
| 91 | + private static function completed( int $subscription_id ) { | |
| 92 | + $comment_id = wp_insert_comment( | |
| 93 | + array( | |
| 94 | + 'comment_author' => 'Subscription for WooCommerce', | |
| 95 | + 'comment_content' => 'Subscription completed. All payments made.', | |
| 96 | + 'comment_post_ID' => $subscription_id, | |
| 97 | + 'comment_type' => 'order_note', | |
| 98 | + ) | |
| 99 | + ); | |
| 100 | + update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Completed' ); | |
| 101 | + update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_completed' ); | |
| 102 | + | |
| 103 | + do_action( 'subscrpt_subscription_completed', $subscription_id ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + /** | |
| 72 | 107 | * Write Comment About expired Subscription. |
| 73 | 108 | * |
| 74 | 109 | * @param int $subscription_id Subscription ID. |
| 75 | 110 | */ |
| @@ -96,9 +131,9 @@ | ||
| 96 | 131 | private static function active( int $subscription_id ) { |
| 97 | 132 | $comment_id = wp_insert_comment( |
| 98 | 133 | array( |
| 99 | 134 | 'comment_author' => 'Subscription for WooCommerce', |
| 100 | - 'comment_content' => 'Subscription activated.Next payment due date set.', | |
| 135 | + 'comment_content' => 'Subscription activated. Next payment due date set.', | |
| 101 | 136 | 'comment_post_ID' => $subscription_id, |
| 102 | 137 | 'comment_type' => 'order_note', |
| 103 | 138 | ) |
| 104 | 139 | ); |
| @@ -188,8 +223,14 @@ | ||
| 188 | 223 | ) |
| 189 | 224 | ); |
| 190 | 225 | update_comment_meta( $comment_id, '_subscrpt_activity', 'Subscription Pending Cancellation' ); |
| 191 | 226 | update_comment_meta( $comment_id, '_subscrpt_activity_type', 'subs_pe_cancel' ); |
| 227 | + | |
| 228 | + // WC_Email classes only exist once the mailer has been built, and they | |
| 229 | + // attach their own listeners from their constructors. Without this, an | |
| 230 | + // email listening for a pending cancellation is simply not registered yet | |
| 231 | + // when the action fires - the same reason cancelled() calls it. | |
| 232 | + WC()->mailer(); | |
| 192 | 233 | |
| 193 | 234 | do_action( 'subscrpt_subscription_pending_cancellation', $subscription_id ); |
| 194 | 235 | } |
| 195 | 236 | } |