REST API: Adding + removing coupons via API doesn't recalculate totals correctly #18236
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.
This PR is proof of an issue, rather than a fix in itself. When coupons are added or removed from an existing order via the API, the item totals are not updated. I've added two failing tests in this PR, one for adding & one for removing a coupon.
You can also replicate the issue manually, by sending requests to
POST orders/:orderId
Add a coupon:
Remove a coupon:
In both cases, the line items are not updated (the discount is not applied to line items, or it's not removed from line items), which means the order total is not updated again when
calculate_totals
is run. It looks like a fix might be to runWC_Order::recalculate_coupons
ifcoupon_lines
changes, but that's a protected function in the abstract class. An alternate approach could be to use the apply_coupon/remove_coupon functions directly for coupon_lines.Feel free to take over this PR to fix this, or give me a pointer & I'll fix it. We'll probably end up porting whatever fix we end up with into https://github.com/woocommerce/wc-api-dev, so letting @justinshreve & @timmyc know this exists :)