API error on Create order - 422 Unprocessable Entity - 'must have at least one line item'

Solved
Rakesh_Dobariya
Shopify Partner
11 0 8

Hi,

 

I am read all document and forum post regarding creating order with live item level discount, but none of them are working for me.

 

Here is my JSON payload.

 

{
   "order":{
      "discount_codes":[
         {
            "code":"RAKESHTE2ST",
            "amount":"45"
         }
      ],
      "email":"foo@example.com",
      "line_items":[
         {
            "variant_id":21516219908176,
            "quantity":1,
            "discount_allocations": [
          {
            "amount": "45",
            "discount_application_index": 0
          }
        ]
         },
         {
            "variant_id":21516220006480,
            "quantity":1
         }
      ]
   }
}

Discount code is configured to give 30% on one of variant that I mentioned in line items.

 

Expected result is having discount on that 21516219908176 ( variant ID ) not on other. But its being applied prorated on both line items. 

 

Can any one help me please ?

 

Thanks,

Rakesh

Integration Agents
Accepted Solution (1)

Accepted Solutions
Josh
Shopify Staff
Shopify Staff
1134 82 213

This is an accepted solution.

Hey Rakesh, 

 

The current implementation of discounts with the Order API only allows discount codes to be applied to the whole order, not at a line item level. 

 

The discount_allocations array is also read only, so it will be ignored when you include it in your calls. When using your example call you provided, it could be altered like this so that discount_allocations is no longer present : 

 

{
   "order":{
      "discount_codes":[
         {
            "code":"RAKESHTE2ST",
            "amount":"45"
         }
      ],
      "email":"foo@example.com",
      "line_items":[
         {
            "variant_id":21516219908176,
            "quantity":1,
         },
         {
            "variant_id":21516220006480,
            "quantity":1
         }
      ]
   }
}

That would apply a flat discount of $45 to the entire order. 

 

The DraftOrder API does support discounts at a line item level though, so you may want to experiment with creating draft orders instead and then converting them into completed orders afterwards.

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

View solution in original post

Reply 1 (1)
Josh
Shopify Staff
Shopify Staff
1134 82 213

This is an accepted solution.

Hey Rakesh, 

 

The current implementation of discounts with the Order API only allows discount codes to be applied to the whole order, not at a line item level. 

 

The discount_allocations array is also read only, so it will be ignored when you include it in your calls. When using your example call you provided, it could be altered like this so that discount_allocations is no longer present : 

 

{
   "order":{
      "discount_codes":[
         {
            "code":"RAKESHTE2ST",
            "amount":"45"
         }
      ],
      "email":"foo@example.com",
      "line_items":[
         {
            "variant_id":21516219908176,
            "quantity":1,
         },
         {
            "variant_id":21516220006480,
            "quantity":1
         }
      ]
   }
}

That would apply a flat discount of $45 to the entire order. 

 

The DraftOrder API does support discounts at a line item level though, so you may want to experiment with creating draft orders instead and then converting them into completed orders afterwards.

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog