Automattic / woocommerce-subscriptions-core

Subscriptions core package for WooCommerce
Other
87 stars 33 forks source link

Fix issues with discounts being applied to manually created parent orders #439

Open james-allan opened 1 year ago

james-allan commented 1 year ago

Fixes woocommerce-subscriptions/issues/4504

🛑 This PR is on-hold while more work and testing is done. I've submitted it as a draft for now.

Description

When you apply a coupon to a subscription manually and then pay for a newly created subscription parent order, the discount is supposed to be copied into into the cart as a coupon. Because of a rounding discrepancy with how WC core stores coupon line item totals and order discounts, this was not working as intended. What's more, because this wasn't working as intended, customers would end up in a flow that had even more issues.

This PR fixes those issues. Namely:

  1. When the order discount total doesn't match the coupon line item total because of a rounding discrepancy, allow for a delta of 1 unit of precision (eg 1 cent). This allows us to use the actual coupon rather than having to retrofit a custom coupon to match the actual total.
  2. When using a custom coupon (eg when a discount is applied to the line item manually), make sure we use the discount total + the discount tax, otherwise the total will be incorrect.
  3. 🕙 TODO when a custom coupon is applied, it should also apply to the subscription if the same discount also applies to the subscription.

How to test this PR

Set up:

  1. Create a 50% Recurring Product % Discount coupon
  2. Create a $10 monthly subscription
    • This 50% and $10 combination is important as it leads to the rounding discrepancy.
  3. Store Tax settings: prices entered including taxes and a 10% store tax applies
  4. manually create a subscription
  5. Add the product, recalculate taxes, apply the coupon. The amount is right.
  6. Create a pending order
  7. click the pay for order link
    1. On trunk the order total will be incorrect ($5.46) and the recurring cart won't have the coupon applied.
    2. On this branch the total should be correct and the coupon should apply to both the initial amount and recurring cart.

Further tests should be ran to make sure different tax, amounts and ways of applying discounts to parent orders and subscriptions should be tested.

Product impact