How to figure out resulting Order Line Item price

Hi!

We are trying to figure out the formula/logic how to calculate resulting price of the Order Line Item - the same value seen in the Shopify back-end UI.

Admin REST API does not return the resulting price - instead it returns the following values which put together can probably get the expected result but there is no documented formula/logic for that:

  • price
  • total_discount
  • discount_allocations

We have 2x different Shopify instances in context where we see the following from Shopify Admin REST API “/orders/$id.json” response:

  1. in one case:
    • price is 130 EUR
    • total_discount is 0.00
    • discount_allocations array has a single entry of 26 EUR
      • which goes in hand with the value seen in the UI => 104 EUR
  2. in another case
    • price is 45 EUR
    • total_discount is 45 EUR
    • discount_allocations array has a single entry of 45 EUR
      • which goes in hand with the value seen in the UI => 0 EUR (it is a freebie)

We would assume based on the naming alone the formula could be: price - total_discount = resulting price but this is clearly not the case. We could assume that deducting all the discount_allocations entries would give us the result obviously we cant be 100% sure (there could be some other edge cases etc.). Its very weird that mentioned API does not has an attribute which represents the resulting price to be used as-is.

We would very much appreciate a valid 100% correct formula/logic how to determine the resulting price of each Order Line Item.

Hi, did you manage to solve this somehow or find a workaround?

Support confirmed that the base formula is

((p.original_price * p.quantity * (1- p.discount_percent)) / p.quantity).trunc_with_scale(2);

But in addition to that there is a round robin system to assign missing cents if (sum_of_discounted_lines_original_prices * discount != sum(discounted_lines_discounted_prices)).

Happens for us with a 20% discount on 8.49, 6.49, 6.49: The expected total is 17.178 → 17.18, but each position round individually gives a wrong result:
8.49 * 0.8 = 6.792 → 6.79
6,49 * 0.8 = 5.192 → 5.19
6,49 * 0.8 = 5.192 → 5.19
6.79 + 5.19 + 5.19 = 17.17 → one cent is missing.

That missing cent gets added to the last position in this case…