Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Re: How to create a 30% order discount code for 24 hours with Shopify API

How to create a 30% order discount code for 24 hours with Shopify API

dmitry_z
Shopify Partner
68 0 7

Hey guys, need help with Shopify API - Price Rule (Discount). 

I'm building a chatbot flow with Zapier and want to create a 24 hours, 30% off discount code only when user clicks "I want discount" button. 

I'm a little lost on how do I do it.
How do I create a discount that is set for 24 hours from the time user clicks the button?

All the guides just reference creating a discount code without a time limit or time limit from the day I create the price rule (discount), that, how I understand, needs to be created prior.

Any help would be appreciated!

- Dmitry
Replies 2 (2)

dmitry_z
Shopify Partner
68 0 7

I went withGraphQL API and Zapier Weebhooks, and wrote the following in the Data field:

 

{
  "query": "mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) { discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) { codeDiscountNode { codeDiscount { ... on DiscountCodeBasic { title codes(first: 10) { nodes { code } } startsAt endsAt customerSelection { ... on DiscountCustomerAll { allCustomers } } customerGets { value { ... on DiscountPercentage { percentage } } items { ... on AllDiscountItems { allItems } } } appliesOncePerCustomer } } } userErrors { field code message } } }",
  "variables": {
    "basicCodeDiscount": {
      "title": "30% off Baby Model 24 Hrs Only",
      "code": "{{_GEN_1707882510278__randomCode}}",
      "startsAt": "{{_GEN_1707882510278__startDate}}",
      "endsAt": "{{_GEN_1707882510278__endDate}}",
      "customerSelection": {
        "all": true
      },
      "customerGets": {
        "value": {
          "percentage": 0.3
        },
        "items": {
          "all": true
        }
      },
      "appliesOncePerCustomer": true
    }
  }
}

 


Where Code, StartsAt, EndsAt are pulled from previous step generated by JS. 
Looks like this:
"code": CODE3753
"startsAt": 2024-02-13T21:17:32.991Z
"endsAt": 2024-02-14T21:17:32.991Z

I tried static data as well.

Headers:
Content-Type
application/json

X-Shopify-Access-Token
shXXpat_fd94cc6303bc0adcc0895bacXXXXX

Added: write/read discount permissions in custom app.

API Version:
admin/api/2024-01/graphql.json 

Result:
Failed to create a request in Webhooks by Zapier
Bad Request (HTTP Status Code: 400) 😢

- Dmitry
jamalsoueidan
Shopify Partner
66 3 10

install graphql as app inside your shopify store, and test the query there, then you can figure out what the problem is easily.