Orders API: Mapping Discount Applications to Line Items (Rest API)

Solved

Orders API: Mapping Discount Applications to Line Items (Rest API)

API_Ghost
Visitor
3 0 0

Hello,

I'm having trouble connecting discounts_applications to line_items in the Rest API (Orders API, Get Specific Order endpoint). Here's what I'm hoping to do in psuedocode.

 

If orders[x].discount_applications[y].title == "target value" then

     runningDiscountTotal += orders[x].line_items[?].total_discount

 

The value for comparison only seems to be in the discount_allocations.title. The problem is the discount_applications and line_items are not necessarily in the same order and there doesn't seem to be a way to link them in the json. Is there a way to do what I'm looking for?

Accepted Solution (1)

Kyle_liu
Shopify Partner
183 26 30

This is an accepted solution.

Hi, @API_Ghost 

As shown in the figure, the value of 'discount_application_index' can serve as a criterion for judgment.

Kyle_liu_0-1717749081309.png

 

If this is helpful, please Like and Accept the solution.
Want to modify or custom changes on store? Let me help.
- Feel free to contact me on ([email protected])

View solution in original post

Replies 4 (4)

Kyle_liu
Shopify Partner
183 26 30

This is an accepted solution.

Hi, @API_Ghost 

As shown in the figure, the value of 'discount_application_index' can serve as a criterion for judgment.

Kyle_liu_0-1717749081309.png

 

If this is helpful, please Like and Accept the solution.
Want to modify or custom changes on store? Let me help.
- Feel free to contact me on ([email protected])
API_Ghost
Visitor
3 0 0

This is exactly what I was looking for! Thank you! 

shinono
Shopify Partner
7 0 0

is there a chance to get some code to see how you handle the line times? i try to get close to the sales report, but with refund etc it's a bit difficult for me. thanks for yout help @API_Ghost 

API_Ghost
Visitor
3 0 0

Does this help?

i is the index from the initial for loop that looked for the title. If a match is found, it runs the below code.

 

              //START MATCH PROCESSING ============================================
              for (let j = 0; j < orderDetails.data.orders[orderIndexLocation].line_items.length; j++)
                {
                  for (let k = 0; k < orderDetails.data.orders[orderIndexLocation].line_items[j].discount_allocations.length; k++)
                    {
                      if (orderDetails.data.orders[orderIndexLocation].line_items[j].discount_allocations[k].discount_application_index == i)
                        {
                          tempTotal += parseFloat(orderDetails.data.orders[orderIndexLocation].line_items[j].discount_allocations[k].amount)
                        }
                    }
                }
              //STOP MATCH PROCESSING =============================================