Anchor to discountCodeBxgyCreatediscount
discountCodeBxgyCreate
mutation
Requires Apps must have access scope.
Creates a buy X get Y discount (BXGY) that's applied on a cart and at checkout when a customer enters a code.
Anchor to Arguments
Arguments
- Anchor to bxgyCodeDiscountbxgy•
Code Discount DiscountCode requiredBxgy Input! The input data used to create the BXGY code discount.
Was this section helpful?
Anchor to DiscountCodeBxgyCreatePayload returnsDiscountCodeBxgyCreatePayload returns
- Anchor to codeDiscountNodecode•
Discount Node The code discount that was created.
- Anchor to userErrorsuser•
Errors [DiscountUser non-nullError!]! The list of errors that occurred from executing the mutation.
Was this section helpful?
- Create a basic Buy X get Y (BXGY) discount
- discountCodeBxgyCreate reference
Examples
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
curl -X POST \
https://your-development-store.myshopify.com/admin/api/2024-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Access-Token: {access_token}' \
-d '{
"query": "mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) { discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) { codeDiscountNode { codeDiscount { ... on DiscountCodeBxgy { title codes(first: 10) { nodes { code } } startsAt endsAt customerBuys { items { ...collectionsFragment } value { ... on DiscountQuantity { quantity } } } customerGets { appliesOnOneTimePurchase appliesOnSubscription value { ... on DiscountOnQuantity { effect { ... on DiscountPercentage { percentage } } quantity { quantity } } } items { ...collectionsFragment } } customerSelection { ... on DiscountCustomerAll { allCustomers } } appliesOncePerCustomer usesPerOrderLimit } } } userErrors { field code message } } } fragment collectionsFragment on DiscountCollections { collections(first: 10) { nodes { id title } } }",
"variables": {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2022-09-21T00:00:00Z",
"startsAt": "2022-06-21T00:00:00Z",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
}
}'
const { admin } = await authenticate.admin(request);
const response = await admin.graphql(
`#graphql
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
{
variables: {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2022-09-21T00:00:00Z",
"startsAt": "2022-06-21T00:00:00Z",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
},
},
);
const data = await response.json();
const client = new shopify.clients.Graphql({session});
const data = await client.query({
data: {
"query": `mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}`,
"variables": {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2022-09-21T00:00:00Z",
"startsAt": "2022-06-21T00:00:00Z",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
},
},
});
session = ShopifyAPI::Auth::Session.new(
shop: "your-development-store.myshopify.com",
access_token: access_token
)
client = ShopifyAPI::Clients::Graphql::Admin.new(
session: session
)
query = <<~QUERY
mutation discountCodeBxgyCreate($bxgyCodeDiscount: DiscountCodeBxgyInput!) {
discountCodeBxgyCreate(bxgyCodeDiscount: $bxgyCodeDiscount) {
codeDiscountNode {
codeDiscount {
... on DiscountCodeBxgy {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerBuys {
items {
...collectionsFragment
}
value {
... on DiscountQuantity {
quantity
}
}
}
customerGets {
appliesOnOneTimePurchase
appliesOnSubscription
value {
... on DiscountOnQuantity {
effect {
... on DiscountPercentage {
percentage
}
}
quantity {
quantity
}
}
}
items {
...collectionsFragment
}
}
customerSelection {
... on DiscountCustomerAll {
allCustomers
}
}
appliesOncePerCustomer
usesPerOrderLimit
}
}
}
userErrors {
field
code
message
}
}
}
fragment collectionsFragment on DiscountCollections {
collections(first: 10) {
nodes {
id
title
}
}
}
QUERY
variables = {
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": ["gid://shopify/Collection/1007901140"]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": ["gid://shopify/Collection/711838235"]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
"all": true
},
"endsAt": "2022-09-21T00:00:00Z",
"startsAt": "2022-06-21T00:00:00Z",
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"usesPerOrderLimit": 3
}
}
response = client.query(query: query, variables: variables)
Input variables
JSON{
"bxgyCodeDiscount": {
"code": "SUMMERSALE",
"customerBuys": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/1007901140"
]
}
},
"value": {
"quantity": "3"
}
},
"customerGets": {
"items": {
"collections": {
"add": [
"gid://shopify/Collection/711838235"
]
}
},
"value": {
"discountOnQuantity": {
"effect": {
"percentage": 0.2
},
"quantity": "2"
}
}
},
"customerSelection": {
Response
JSON{
"discountCodeBxgyCreate": {
"codeDiscountNode": {
"codeDiscount": {
"title": "20% off up to two snowboards that are on sale for every three featured snowboards you buy.",
"codes": {
"nodes": [
{
"code": "SUMMERSALE"
}
]
},
"startsAt": "2022-06-21T00:00:00Z",
"endsAt": "2022-09-21T00:00:00Z",
"customerBuys": {
"items": {
"collections": {
"nodes": [
{
"id": "gid://shopify/Collection/1007901140",
"title": "Featured items"
}
]