Anchor to section titled 'undefined'

checkoutDiscountCodeApplyV2
mutation
deprecated

Requires unauthenticated_write_checkouts access scope.

Applies a discount to an existing checkout using a discount code. The Storefront GraphQL Checkout API is deprecated and will be removed in a future version. Please see https://shopify.dev/changelog/deprecation-of-checkout-apis for more information.


Anchor to checkoutId
checkoutId
required

The ID of the checkout.

Anchor to discountCode
discountCode
required

The discount code to apply to the checkout.


Was this section helpful?

The updated checkout object.

The list of errors that occurred from executing the mutation.

The list of errors that occurred from executing the mutation. Use checkoutUserErrors instead.


Was this section helpful?

Examples

Hide code
Copy
mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {
  checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {
    checkout {
      discountApplications(first: 10) {
        edges {
          node {
            allocationMethod
            targetSelection
            targetType
          }
        }
      }
    }
    checkoutUserErrors {
      message
      code
      field
    }
  }
}
curl -X POST \
https://your-development-store.myshopify.com/api/2024-04/graphql.json \
-H 'Content-Type: application/json' \
-H 'X-Shopify-Storefront-Access-Token: {storefront_access_token}' \
-d '{
"query": "mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) { checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) { checkout { discountApplications(first: 10) { edges { node { allocationMethod targetSelection targetType } } } } checkoutUserErrors { message code field } } }",
 "variables": {
    "checkoutId": "gid://shopify/Checkout/9234567890qwerty",
    "discountCode": "fixed_amount_off_code"
  }
}'
const { storefront } = await unauthenticated.storefront(
  'your-development-store.myshopify.com'
);

const response = await storefront.graphql(
  `#graphql
  mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {
    checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {
      checkout {
        discountApplications(first: 10) {
          edges {
            node {
              allocationMethod
              targetSelection
              targetType
            }
          }
        }
      }
      checkoutUserErrors {
        message
        code
        field
      }
    }
  }`,
  {
    variables: {
      "checkoutId": "gid://shopify/Checkout/9234567890qwerty",
      "discountCode": "fixed_amount_off_code"
    },
  },
);

const data = await response.json();
const client = new shopify.clients.Storefront({
  domain: 'your-development-store.myshopify.com',
  storefrontAccessToken,
});
const data = await client.query({
  data: {
    "query": `mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {
      checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {
        checkout {
          discountApplications(first: 10) {
            edges {
              node {
                allocationMethod
                targetSelection
                targetType
              }
            }
          }
        }
        checkoutUserErrors {
          message
          code
          field
        }
      }
    }`,
    "variables": {
      "checkoutId": "gid://shopify/Checkout/9234567890qwerty",
      "discountCode": "fixed_amount_off_code"
    },
  },
});
use Shopify\Clients\Graphql;

$client = new ShopifyClientsStorefront("your-development-store.myshopify.com", $storefrontAccessToken);
$query = <<<QUERY
  mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {
    checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {
      checkout {
        discountApplications(first: 10) {
          edges {
            node {
              allocationMethod
              targetSelection
              targetType
            }
          }
        }
      }
      checkoutUserErrors {
        message
        code
        field
      }
    }
  }
QUERY;

$variables = [
  "checkoutId" => "gid://shopify/Checkout/9234567890qwerty",
  "discountCode" => "fixed_amount_off_code",
];

$response = $client->query(["query" => $query, "variables" => $variables]);
import {useShopQuery} from '@shopify/hydrogen';

const QUERY = gql`
  mutation applyDiscountCodeToCheckout($checkoutId: ID!, $discountCode: String!) {
  checkoutDiscountCodeApplyV2(checkoutId: $checkoutId, discountCode: $discountCode) {
    checkout {
      discountApplications(first: 10) {
        edges {
          node {
            allocationMethod
            targetSelection
            targetType
          }
        }
      }
    }
    checkoutUserErrors {
      message
      code
      field
    }
  }
}`;

const {data} = useShopQuery({
  query: QUERY,
  variables: {
    checkoutId: "gid://shopify/Checkout/9234567890qwerty",
    discountCode: "fixed_amount_off_code"
  },
});
Hide code
Input variables
Copy
{
  "checkoutId": "gid://shopify/Checkout/9234567890qwerty",
  "discountCode": "fixed_amount_off_code"
}
Hide code
Response
JSON
{
  "checkoutDiscountCodeApplyV2": {
    "checkout": {
      "discountApplications": {
        "edges": [
          {
            "node": {
              "allocationMethod": "ACROSS",
              "targetSelection": "ALL",
              "targetType": "LINE_ITEM"
            }
          }
        ]
      }
    },
    "checkoutUserErrors": []
  }
}