Access to the Discount codes with GraphQL and Storefront

Highlighted
Excursionist
18 1 1

Hello,

 

I'm trying to execute a query of priceRule to get the discount codes in my store. The query works fine in GraphiQL in my admin page, however, in GraphiQL IDE I get this error: DiscountCodeSortKeys isn't a defined input type (on $code) . Is it because of the permissions offered by Storefront? And is there a possibility to get a list of the discount codes?

 

Here is the query:

 

query getDiscount($code: DiscountCodeSortKeys) {
priceRules(first: 1) {
edges {
node {
discountCodes(first: 1, sortKey: $code) {
edges {
node {
code
id
}
}
}
}
}
}
}

 Query variable:

 

{"code":"FS"}

 

0 Likes
Excursionist
18 1 1

Any thoughts?

 

I found an answer here.

It means that the Storefront API doesn't have a priceRule field, so I have to use the 'discountApplications' connection to get I want.

What does this mean? Is there any example of that?

0 Likes
Shopify Staff
Shopify Staff
472 35 91

Hi @amir_nassal 

 

What info are you trying to get?  A list of discount codes on your Store?  You wouldn't be able to accomplish this using the Storefront API.  You would need to use the Admin API (REST or GraphQL).  If you can provide more specifics I can share an example call.

 

DiscountApplications in the Storefront API that you mentioned is only an object that exists on a specific Checkout.  This lets you see the code applied to that checkout.

 

Cheers,

Ryan

Developer Experience @ Shopify
0 Likes
Excursionist
18 1 1

Hi @Ryan ,

 

Thank you for your reply.

I have a custom liquid page and I want to check based on the user's input, if the discount code exists in the store or not.

I've already tested it for products and it's working.

I would appreciate it if you could give me an example to achieve that.

 

Here is the Javascript code:

 

let txt = document.getElementById("discount-input").value;
let getDiscount = ` { priceRules(first: 1, query: "discount_code:*`+ txt +`*") { edges { node { id title status valueV2 { __typename ... on PricingPercentageValue { percentage } } } } } } `; let settings = { 'async': true, 'crossDomain': true, 'url': 'https://example-page.myshopify.com/api/graphql', 'method': 'POST', 'headers': { 'X-Shopify-Storefront-Access-Token': 'xxxxxxxxxxxx', 'Content-Type': 'application/graphql', }, 'data': getDiscount }; return $.ajax(settings).done(function (response) { $.each(response.data.priceRules.edges[0], function(i, item){ console.log(item.title); }); });

 

0 Likes
Shopify Staff
Shopify Staff
472 35 91

Hi Amir,

 

Unfortunately via GraphQL there isn't a great way to reverse lookup by code.  You can look at all existing codes and parse the results to find the code with a simple GraphQL query similar to: 

 

{
  priceRules (first:10) {
    edges {
      node {
        id
        discountCodes (first:10) {
          edges {
            node {
              code
              id
            }
          }
        }
      }
    }
  }
}

 

In REST we do have a lookup endpoint that could solve your problem: https://help.shopify.com/en/api/reference/discounts/discountcode#lookup

Your app can make a GET call to this endpoint and receive the full resource structure as a result.

Developer Experience @ Shopify
0 Likes
Excursionist
18 1 1

Hi @Ryan 

 

Yes, that query works in GraphiQl app in my admin page, however, it doesn't work in my Javascript code linked to the liquid page in the templates folder, because the priceRule field does not exist in the Storefront API.

I tried the "REST" solution that you suggested but it asks for administration information (username and password) which is not good for me because this is a public page in the store.

 

Is there a similar method to get the discount codes or at least avoid the username and password input?

 

Thank you.

 

0 Likes
Shopify Staff
Shopify Staff
472 35 91
No, you cannot get access to discount codes without being authorized to use
the admin API on that shop. You can see how that would be a problem if
anyone was able to access it.
Developer Experience @ Shopify
0 Likes