Have your say in Community Polls: What was/is your greatest motivation to start your own business?
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.

Adding multiple codes to GraphQL Discount Code Query

Adding multiple codes to GraphQL Discount Code Query

Coral_Solution
Shopify Partner
1 0 2

Hey All, 

 

I am trying to create discount codes programmatically in a shopify store, using the GraphQL requests. The requests are successful & I can see the codes being generated in the store. I have provided the screenshots below. The GraphQL requests I am using is also shared below.

 

As you can see in the Screenshot 3, for every request I make a single discount code "12345678" gets created. Lets say under "code basic test", I want to add multiple unique codes like "12345678", "abcd", "wxyz" etc.; is it doable? This way I can generate many unique codes where all codes will follow the "code basic test" discount rules. 

  1. Is there any way to create multiple codes under the "code basic test" container?
  2. I would like to generate these codes on an ongoing basis i.e. would prefer to add codes on a daily basis rather than to create all codes one time. I tried using the update mutation (request payload shared below) & what it does is, it overwrites the existing code with a new code rather than adding a new code.

The reason I need this is because:

  1. I need to create unique bulk codes. If I create a new "code basic test" discount code rule every time I generate a code, there will be 100s of records in the Shopify Discounts section. This will not be a good experience.
  2. Plus, the maintenance of codes will be difficult. I would rather prefer a single record which I can modify anytime & all the associated codes "12345678", "abcd", "wxyz" etc. will get updated.

If possible, Please share the GraphQL request to add the codes. Thanks!

 

Newly Created Code in Discounts Section

Screenshot 1

Forum_3.png

Screenshot 2

Forum_1.png

Screenshot 3

Forum_2.png

 

Create Code GraphQL Request

 

mutation {
  discountCodeBasicCreate(basicCodeDiscount: {
    title: "code basic test",
    startsAt: "2019-01-01",
    endsAt: "2023-01-01",
    customerSelection: {
      all: true
    }
    code: "12345678",
    customerGets: {
      value: {
        discountAmount:  {
          amount: 1.00,
          appliesOnEachItem: false
        }
      }
      items: {
          all: true
      }
    }}) {
    userErrors { field message code }
    codeDiscountNode {
      id
        codeDiscount {
        ... on DiscountCodeBasic {
          title
          summary
          status
          codes (first:10) {
            edges {
              node {
                code
              }
            }
          }
        }
      }
    }
  }
}

 

 

Update GraphQL Request

mutation {
  discountCodeBasicUpdate(id: "gid://shopify/DiscountCodeNode/1167105949949",
    basicCodeDiscount: {
      code: "xyzt5644261"
    }) {
    userErrors { field message code }
    codeDiscountNode {
      id
      codeDiscount {
        ... on DiscountCodeBasic {
          shareableUrls {
              targetType,
              title,
              url
          }
          title
          summary
          status
          codes (first:100) {
            edges {
              node {
                code
              }
            }
          }
        }
      }
    }
  }
}

 

Replies 3 (3)

sublimina
Shopify Partner
5 0 2

I had the same problem and realized that the REST api will let you create multiple codes using a price rule. So first, create a price rule. Then, you can create a batch of discount codes from the rule.

https://shopify.dev/api/admin-rest/2022-04/resources/pricerule#post-price-rules

https://shopify.dev/api/admin-rest/2022-04/resources/discountcode#post-price-rules-price-rule-id-bat...

ericnam
Shopify Partner
9 0 11

The REST API does work but is missing key properties that the new GQL Discount Code type offers. It seems like Shopify is moving away from REST in general and using GQL; however the PriceRule in the GQL API is being deprecated.

 

From what I can see from the docs, there is no way to tie in more than one code under one ruleset; however, this should be supported.

ko-lem
Shopify Partner
15 0 10

There's not even a way of doing this through the UI..