FROM CACHE - en_header

Discount codes for one-time purchases and subscriptions

Solved
falex28
New Member
1 0 1

Hi,

Currently, all discount codes created via Discount APIs are working only for one-time purchases https://shopify.dev/api/admin/rest/reference/discounts

There is an option on Shopify Admin to change to "Both" - it means discount codes will work on subscriptions too.

image_2021_08_25T08_44_41_340Z.png

 

 

 

 

 

I want to do this via API, but I cannot find the purchase type property on the PriceRule resource https://shopify.dev/api/admin/rest/reference/discounts/pricerule

It is possible to create a discount code via API which will work for one-time purchases and subscriptions too?

Accepted Solution (1)

Accepted Solutions
csam
Shopify Staff
Shopify Staff
267 40 45

This is an accepted solution.

Hi @falex28 and @Conjured_Char 

It is possible to create discount codes which apply to subscriptions using the GraphQL API. If you're not yet familiar with the GraphQL API guide we have a helpful Getting Started guide

I was able to create a discount code which applies to both subscription orders and regular products using the following mutation: 

mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
  discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
    codeDiscountNode {
      id
    }
    userErrors {
      code
      extraInfo
      field
      message
    }
  }
}

with variables: 

{
  "basicCodeDiscount": { 
    "code": "TESTCODE",
    "title": "testing subscription code creation",
    "startsAt": "2021-07-03T20:47:55Z",
    "customerSelection": {"all": true}, 
    "customerGets": {
      "items": { "collections": 
        { "collectionIds": "gid:\/\/shopify\/Collection\/1234567890" }
      },
      "appliesOnSubscription": true,
      "appliesOnOneTimePurchase": true,
      "value": {
        "discountAmount": {
          "amount": 100,
          "appliesOnEachItem": true
      }
    }
    } 
  }
}

 

You can also check out the examples here - https://shopify.dev/api/examples/discounts You can add the "appliesOnSubscription" and "appliesOnOneTimePurchase" fields to the "customerGets" object shown in the example.

Here are the documentation pages relevant to the request: 

DiscountCodeBasic - https://shopify.dev/api/admin/graphql/reference/discounts-and-marketing/discountcodebasic

discountCodeBasicCreate - https://shopify.dev/api/admin/graphql/reference/discounts-and-marketing/discountcodebasiccreate 

DiscountCustomerGetsInput - https://shopify.dev/api/admin/graphql/reference/discounts-and-marketing/discountcustomergetsinput 

CS | API Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Replies 6 (6)
Conjured_Char
Shopify Partner
33 0 3

I have the same problem/question and would love an answer. We've posted to the partner Slack and contacted Shopify support and were told to post here instead, but it looks like Shopify hasn't replied to any of the topics started with this question yet (over the last few months).

csam
Shopify Staff
Shopify Staff
267 40 45

This is an accepted solution.

Hi @falex28 and @Conjured_Char 

It is possible to create discount codes which apply to subscriptions using the GraphQL API. If you're not yet familiar with the GraphQL API guide we have a helpful Getting Started guide

I was able to create a discount code which applies to both subscription orders and regular products using the following mutation: 

mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
  discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
    codeDiscountNode {
      id
    }
    userErrors {
      code
      extraInfo
      field
      message
    }
  }
}

with variables: 

{
  "basicCodeDiscount": { 
    "code": "TESTCODE",
    "title": "testing subscription code creation",
    "startsAt": "2021-07-03T20:47:55Z",
    "customerSelection": {"all": true}, 
    "customerGets": {
      "items": { "collections": 
        { "collectionIds": "gid:\/\/shopify\/Collection\/1234567890" }
      },
      "appliesOnSubscription": true,
      "appliesOnOneTimePurchase": true,
      "value": {
        "discountAmount": {
          "amount": 100,
          "appliesOnEachItem": true
      }
    }
    } 
  }
}

 

You can also check out the examples here - https://shopify.dev/api/examples/discounts You can add the "appliesOnSubscription" and "appliesOnOneTimePurchase" fields to the "customerGets" object shown in the example.

Here are the documentation pages relevant to the request: 

DiscountCodeBasic - https://shopify.dev/api/admin/graphql/reference/discounts-and-marketing/discountcodebasic

discountCodeBasicCreate - https://shopify.dev/api/admin/graphql/reference/discounts-and-marketing/discountcodebasiccreate 

DiscountCustomerGetsInput - https://shopify.dev/api/admin/graphql/reference/discounts-and-marketing/discountcustomergetsinput 

CS | API Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

Conjured_Char
Shopify Partner
33 0 3

@csam Amazing, thanks so much for the reply! Is this an undocumented way of doing it? I don't see appliesOnSubscription listed on the basicCodeDiscount page you listed (https://shopify.dev/api/admin/graphql/reference/discounts-and-marketing/discountcodebasic)

 

Conjured_Char
Shopify Partner
33 0 3

 

Removed - figured it out!

 

csam
Shopify Staff
Shopify Staff
267 40 45

It is documented, just a little tricky to find! The last link in the list shows DiscountCustomerGetsInput which has the fields that go in the customerGets section of discountCodeBasicCreate. appliesOnSubscription is one of the fields for DiscountCustomerGetsInput

21-09-z2ras-gljek

 

 

CS | API Support @ 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 Shopify.dev or the Shopify Web Design and Development Blog

Derek_Morin
Shopify Expert
216 1 34

This doesn't work for me. I used to use the PriceRule API, but it doesn't work with Subscriptions. So I've switched to the Discount API, but:

a) discount is not visible in dashboard
b) i cant apply it the way we used to apply:

https://community.shopify.com/c/shopify-apis-and-sdks/discount-api-problems/m-p/1403465#M74968