Create pending renewal order (improve safety/error handling) #768
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For most active subscriptions, it's possible for the merchant to create a pending renewal order. This is generally done via the Order Actions dropdown:
However, as described in the linked issue, it is possible for an uncaught exception to be raised (this might happen if it is not possible to change the subscription status), which breaks the process and leads the user to a blank screen or error message:
Though the original report cites a problem when the subscription status cannot be changed, on further examination it looked to me like there are 3 points of failure within
WCS_Admin_Meta_Boxes::create_pending_renewal_action_request()
, so I've tried to address all three. However, if we prefer to stay tightly focused only on the potential for an uncaught exception (when the subscription status is changed), I'm more than happy to revise and simplify the PR.The three areas I felt could benefit from additional safety:
WP_Error
will have been raised (if so, we'd hit problems when we subsequently try to call order methods).Therefore, this change mostly involves adding try/catch blocks or conditionals so that we handle things gracefully should the unexpected happen.
Additionally (happy to unwind this if it's not desirable), I decided to consolidate the creation of admin notices and order notes, as it felt like both were useful (the admin notice catches the user's eye, and the order note leaves a permanent record/paper trail in case the notice is missed).
Lastly, the linked issue proposes we remove the create pending renewal order item from the Order Actions dropdown if it looks like creating it will lead to an error. As described above, there are lots of conditions in which this might happen, not just one, so I'm not too sure about the merits of this (and, it seems a fix has already been made to the extension with which this was being experienced). However, more than happy to follow-up and put this in place.
Updates https://github.com/woocommerce/woocommerce-subscriptions/issues/4735
How to test this PR
We're can contrive error conditions for each of the three points of failure by applying the following snippets, one at a time (you could, for instance, place these lines in a mu-plugin file as needed):
Let's do some testing! Create or find a suitable existing subscription (one that is active, has a valid payment method already, etc):
Some open questions
::create_pending_renewal_action_request()
in the same PR?Product impact