Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discount amount is incorrect in /sales/ API endpoint when using variable pricing #8246

Closed
ashleyfae opened this issue Dec 7, 2020 · 0 comments · Fixed by #8249
Closed

Discount amount is incorrect in /sales/ API endpoint when using variable pricing #8246

ashleyfae opened this issue Dec 7, 2020 · 0 comments · Fixed by #8249
Assignees
Milestone

Comments

@ashleyfae
Copy link
Contributor

Bug Report

Expected behavior

The /sales/ endpoint includes a discounts array, which is meant to contain the total discount amount (flat dollar amount) for that order. I expect this number to be accurate.

Actual behavior

When using variable prices and purchasing not the cheapest price option, the discount amount is incorrect.

Steps to reproduce the behavior

  1. Create a variable price product with at least two price options. For example: $3 and $10.
  2. Create a discount code. In my tests I used 25%, but setting isn't relevant.
  3. Purchase the higher priced option. So, $10.
  4. View the output of the /sales/ API endpoint. Look at the discounts array. This amount is incorrect.

In my example:

  • I purchased a $10 product with a 25% off discount.
  • The discounts array shows: array( 'discounts' => 0.75 ). That 0.75 would be correct if I'd purchased the cheapest ($3) price option. But since I purchased $10, it should have been 2.5

Information (if a specific version is affected):

PHP Version:

EDD Version (or branch): master

WordPress Version:

Any other relevant information:

The issue is that the options array in cart details isn't being properly passed through to the discount functions. In the API we use edd_get_cart_item_discount_amount() here: https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/api/class-edd-api-v2.php#L451

The structure of $item looks like this:

array(10) {
  ["item_number"]=>
  array(3) {
    ["id"]=>
    int(157)
    ["quantity"]=>
    int(1)
    ["options"]=>
    array(2) {
      ["quantity"]=>
      int(1)
      ["price_id"]=>
      string(1) "2"
    }
  }
}

Note how options is nested inside item_number.

But when you dig through edd_get_cart_item_discount_amount(), it looks for options inside $item['options'] directly https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/cart/class-edd-cart.php#L645 Which means it's never getting picked up and falls back to using the edd_price meta, which, with variable pricing enabled, is defaulted to the cheapest option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant