Anchor to section titled 'undefined'

discountCodeBasicCreate
mutation

Requires Apps must have write_discounts access scope.

Creates a basic 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 discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
  discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
    codeDiscountNode {
      codeDiscount {
        ... on DiscountCodeBasic {
          title
          codes(first: 10) {
            nodes {
              code
            }
          }
          startsAt
          endsAt
          customerSelection {
            ... on DiscountCustomerAll {
              allCustomers
            }
          }
          customerGets {
            value {
              ... on DiscountPercentage {
                percentage
              }
            }
            items {
              ... on AllDiscountItems {
                allItems
              }
            }
          }
          appliesOncePerCustomer
        }
      }
    }
    userErrors {
      field
      code
      message
    }
  }
}
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 discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) { discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) { codeDiscountNode { codeDiscount { ... on DiscountCodeBasic { title codes(first: 10) { nodes { code } } startsAt endsAt customerSelection { ... on DiscountCustomerAll { allCustomers } } customerGets { value { ... on DiscountPercentage { percentage } } items { ... on AllDiscountItems { allItems } } } appliesOncePerCustomer } } } userErrors { field code message } } }",
 "variables": {
    "basicCodeDiscount": {
      "title": "20% off all items during the summer of 2022",
      "code": "SUMMER20",
      "startsAt": "2022-06-21T00:00:00Z",
      "endsAt": "2022-09-21T00:00:00Z",
      "customerSelection": {
        "all": true
      },
      "customerGets": {
        "value": {
          "percentage": 0.2
        },
        "items": {
          "all": true
        }
      },
      "appliesOncePerCustomer": true
    }
  }
}'
const { admin } = await authenticate.admin(request);

const response = await admin.graphql(
  `#graphql
  mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
    discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
      codeDiscountNode {
        codeDiscount {
          ... on DiscountCodeBasic {
            title
            codes(first: 10) {
              nodes {
                code
              }
            }
            startsAt
            endsAt
            customerSelection {
              ... on DiscountCustomerAll {
                allCustomers
              }
            }
            customerGets {
              value {
                ... on DiscountPercentage {
                  percentage
                }
              }
              items {
                ... on AllDiscountItems {
                  allItems
                }
              }
            }
            appliesOncePerCustomer
          }
        }
      }
      userErrors {
        field
        code
        message
      }
    }
  }`,
  {
    variables: {
      "basicCodeDiscount": {
        "title": "20% off all items during the summer of 2022",
        "code": "SUMMER20",
        "startsAt": "2022-06-21T00:00:00Z",
        "endsAt": "2022-09-21T00:00:00Z",
        "customerSelection": {
          "all": true
        },
        "customerGets": {
          "value": {
            "percentage": 0.2
          },
          "items": {
            "all": true
          }
        },
        "appliesOncePerCustomer": true
      }
    },
  },
);

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 discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
    discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
      codeDiscountNode {
        codeDiscount {
          ... on DiscountCodeBasic {
            title
            codes(first: 10) {
              nodes {
                code
              }
            }
            startsAt
            endsAt
            customerSelection {
              ... on DiscountCustomerAll {
                allCustomers
              }
            }
            customerGets {
              value {
                ... on DiscountPercentage {
                  percentage
                }
              }
              items {
                ... on AllDiscountItems {
                  allItems
                }
              }
            }
            appliesOncePerCustomer
          }
        }
      }
      userErrors {
        field
        code
        message
      }
    }
  }
QUERY

variables = {
  "basicCodeDiscount": {
    "title": "20% off all items during the summer of 2022",
    "code": "SUMMER20",
    "startsAt": "2022-06-21T00:00:00Z",
    "endsAt": "2022-09-21T00:00:00Z",
    "customerSelection": {
      "all": true
    },
    "customerGets": {
      "value": {
        "percentage": 0.2
      },
      "items": {
        "all": true
      }
    },
    "appliesOncePerCustomer": true
  }
}

response = client.query(query: query, variables: variables)
const client = new shopify.clients.Graphql({session});
const data = await client.query({
  data: {
    "query": `mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
      discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
        codeDiscountNode {
          codeDiscount {
            ... on DiscountCodeBasic {
              title
              codes(first: 10) {
                nodes {
                  code
                }
              }
              startsAt
              endsAt
              customerSelection {
                ... on DiscountCustomerAll {
                  allCustomers
                }
              }
              customerGets {
                value {
                  ... on DiscountPercentage {
                    percentage
                  }
                }
                items {
                  ... on AllDiscountItems {
                    allItems
                  }
                }
              }
              appliesOncePerCustomer
            }
          }
        }
        userErrors {
          field
          code
          message
        }
      }
    }`,
    "variables": {
      "basicCodeDiscount": {
        "title": "20% off all items during the summer of 2022",
        "code": "SUMMER20",
        "startsAt": "2022-06-21T00:00:00Z",
        "endsAt": "2022-09-21T00:00:00Z",
        "customerSelection": {
          "all": true
        },
        "customerGets": {
          "value": {
            "percentage": 0.2
          },
          "items": {
            "all": true
          }
        },
        "appliesOncePerCustomer": true
      }
    },
  },
});
use Shopify\Clients\Graphql;

$client = new Graphql("your-development-store.myshopify.com", $accessToken);
$query = <<<QUERY
  mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
    discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
      codeDiscountNode {
        codeDiscount {
          ... on DiscountCodeBasic {
            title
            codes(first: 10) {
              nodes {
                code
              }
            }
            startsAt
            endsAt
            customerSelection {
              ... on DiscountCustomerAll {
                allCustomers
              }
            }
            customerGets {
              value {
                ... on DiscountPercentage {
                  percentage
                }
              }
              items {
                ... on AllDiscountItems {
                  allItems
                }
              }
            }
            appliesOncePerCustomer
          }
        }
      }
      userErrors {
        field
        code
        message
      }
    }
  }
QUERY;

$variables = [
  "basicCodeDiscount" => [
    "title" => "20% off all items during the summer of 2022",
    "code" => "SUMMER20",
    "startsAt" => "2022-06-21T00:00:00Z",
    "endsAt" => "2022-09-21T00:00:00Z",
    "customerSelection" => [
      "all" => true,
    ],
    "customerGets" => [
      "value" => [
        "percentage" => 0.2,
      ],
      "items" => [
        "all" => true,
      ],
    ],
    "appliesOncePerCustomer" => true,
  ],
];

$response = $client->query(["query" => $query, "variables" => $variables]);
Hide code
Input variables
Copy
{
  "basicCodeDiscount": {
    "title": "20% off all items during the summer of 2022",
    "code": "SUMMER20",
    "startsAt": "2022-06-21T00:00:00Z",
    "endsAt": "2022-09-21T00:00:00Z",
    "customerSelection": {
      "all": true
    },
    "customerGets": {
      "value": {
        "percentage": 0.2
      },
      "items": {
        "all": true
      }
    },
    "appliesOncePerCustomer": true
  }
}
Hide code
Response
JSON
{
  "discountCodeBasicCreate": {
    "codeDiscountNode": {
      "codeDiscount": {
        "title": "20% off all items during the summer of 2022",
        "codes": {
          "nodes": [
            {
              "code": "SUMMER20"
            }
          ]
        },
        "startsAt": "2022-06-21T00:00:00Z",
        "endsAt": "2022-09-21T00:00:00Z",
        "customerSelection": {
          "allCustomers": true
        },
        "customerGets": {
          "value": {
            "percentage": 0.2
          },
          "items": {
            "allItems": true
          }
        },
        "appliesOncePerCustomer": true
      }
    },
    "userErrors": []
  }
}