Search code examples
shopifyshopify-app

Shopify keeps erasing applied_discount


I'm using the REST API to try to create a draft order with a single line-item that is 100% discounted.

{
  draft_order: {
    customer: customer,
    note: "this is my note",
    line_items: [
      {
        variant_id: 8935893599248,
        quantity: 1,
        applied_discount: {
          title: 'FREE',
          description: 'FREE Item',
          value_type: 'percentage',
          value: '0.00',
          amount: '0.00',
        }
      }
    ],
    shipping_address: {
      address1: data[:address_1],
      address2: data[:address_2],
      city: data[:city],
      province: data[:state],
      zip: data[:zip_code],
      country_code: data[:country],
      phone: data[:phone]
    }.compact
  }
}

It creates the draft order, but in the response it keeps erasing the discount it and setting it to nil... it is driving me insane. I've tried every combination I can think of. So insanely frustrating


Solution

  • You just need to update the json format if you want to create a draft order for 100% discount you just need to assign value as 100 and amount should be the price of product for which you want to give discount

    Here 123456789 is my product's variant id and price of my product is 99.00, Some of data is not as per your data so only change data , but format must be same

    Here I mentioned customer id , you can skip this index if you want to apply discount for all of your customer , order specify the id or email for any particular customer but format must be same.

    {
      "draft_order": {
        "note":"this is my note",
        "line_items": [
          {
            "variant_id": 123456789,
            "quantity": 1
          }
        ],
        "applied_discount": {
          "title": "FREE",
          "description": "FREE Item",
          "value": "100",
          "value_type": "percentage",
          "amount": "99"
        },
        "customer": {
          "id": 987456321
        },
        "shipping_address": {
          "address1": "123 Amoebobacterieae St",
          "address2": "",
          "city": "Ottawa",
          "company": null,
          "country": "Canada",
          "first_name": "Bob",
          "last_name": "Bobsen",
          "latitude": "45.41634",
          "longitude": "-75.6868",
          "phone": "555-625-1199",
          "province": "Ontario",
          "zip": "K2P0V6",
          "name": "Bob Bobsen",
          "country_code": "CA",
          "province_code": "ON"
        }
      }
    }
    

    You can take reference from here https://help.shopify.com/en/api/reference/orders/draftorder#create-2019-07