Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
80 stars 29 forks source link

Reactivating a subscription when it is pending cancellation causes a PHP error. #592

Closed nealbgit closed 1 month ago

nealbgit commented 2 months ago

Describe the bug

When reactivating a subscription that is pending cancellation, a php error occurs. The error that occurs is: Fatal error: uncaught exception: Subscription #5330796: The end date must occur after the next payment date. in /code/wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/class-wc-subscription.php

To Reproduce

  1. Cancel a subscription, so that it is in Pending Cancellation status
  2. Try to reactivate the subscription

Expected behavior

Apparently, when cancelling a subscription, the Next Payment date is cleared, but the end date remains.

Actual behavior

A subscription should be allowed to be reactivated when it is pending cancellation.

Product impact

Additional context

In looking through the subscriptions plugin code, I found that there was an error here: /wp-content/plugins/woocommerce-subscriptions/vendor/woocommerce/subscriptions-core/includes/class-wc-subscription.php lines: [2434 to 2469] - Break statements needed to be added in the case statement to prevent fall-through behavior. Once this was done, the problem was fixed.

-->

james-allan commented 2 months ago

I haven't been able to replicate this issue @nealbgit. I'm not sure if there's more to this issue.

For some background, when you reactivate a pending cancelled subscription we restore what was the original end date and next payment date which is the current "end" date.

So, when you cancel a subscription we store the current end date in meta so we can restore it later if we need to. The subscription's end date becomes the current next payment date.

When you reactivate a pending-cancelled subscription we reverse that - the subscription's end date is the value we stored in the meta and the next payment date is the current end date.

Given the error you're seeing, you'd have to have a subscription with an end date earlier than the next payment date.

Can you share anything about this subscription? eg like the product billing frequency and length settings.

I think I can probably replicate by following these steps:

  1. Create a monthly subscription which ends in 3 months.
  2. Purchase the subscription.
  3. Cancel the subscription,
  4. set the end date to be like 1 year from now.
  5. Reactivate the subscription.

It will attempt to set the end date to be in 3 months from now and the next payment to be 1 year from now. I can't edit pending-cancelled dates so I don't know how that would have happened.

james-allan commented 1 month ago

Closing this due to inactivity and an inability to replicate. As I mentioned in my last comment, the only way I could see this happening is if you set the end date of a pending-cancelled subscription programmatically and the date you set it to was an invalid date once the subscription was reactivated.