Anchor to section titled 'undefined'

discountCodeBxgyCreate
mutation

Requires Apps must have write_discounts access scope.

Creates a Buy X get Y (BXGY) code discount.


The input data used to create the code discount.


Was this section helpful?

The created code discount.

The list of errors that occurred from executing the mutation.


Was this section helpful?

Examples

Hide code
DescriptionCopy
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-01/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();
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)
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
      }
    },
  },
});
use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$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]);
Hide code
Input variables
Copy
{
  "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
  }
}
Hide code
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"
                }
              ]
            }
          },
          "value": {
            "quantity": "3"
          }
        },
        "customerGets": {
          "appliesOnOneTimePurchase": true,
          "appliesOnSubscription": false,
          "value": {
            "effect": {
              "percentage": 0.2
            },
            "quantity": {
              "quantity": "2"
            }
          },
          "items": {
            "collections": {
              "nodes": [
                {
                  "id": "gid://shopify/Collection/711838235",
                  "title": "On Sale!"
                }
              ]
            }
          }
        },
        "customerSelection": {
          "allCustomers": true
        },
        "appliesOncePerCustomer": false,
        "usesPerOrderLimit": 3
      }
    },
    "userErrors": []
  }
}