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"
},
});