Need to create a discount code to a specific cutomer using graphql

Topic summary

Issue: Creating a discount code for a specific customer using Shopify’s GraphQL API. The original mutation had formatting problems that prevented it from working correctly.

Solution provided:

  • Use the discountCodeBasicCreate mutation with proper structure
  • Key parameters include: discount title, code, start/end dates, customer selection, discount value (percentage), and items scope
  • For customer-specific discounts: use customerSelection with customers: { add: ["gid://shopify/Customer/CUSTOMER_ID"] }
  • Set appliesOncePerCustomer: true to limit usage

Code example shared:

customerSelection: {
  customers: {
    add: ["gid://shopify/Customer/12345678"]
  }
}

Outcome: The corrected mutation format resolved the issue. The key was replacing the customer ID with the proper GID format (gid://shopify/Customer/[ID]).

Summarized with AI on November 7. AI used: claude-sonnet-4-5-20250929.

$query = <<<‘QUERY’
mutation discountCodeBasicCreate($basicCodeDiscount: DiscountCodeBasicInput!) {
discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
codeDiscountNode {
codeDiscount {
… on DiscountCodeBasic {
title
codes(first: 10) {
nodes {
code
}
}
startsAt
endsAt
customerSelection {
… on DiscountCustomers {
customers
}
customerGets {
value {
… on DiscountPercentage {
percentage
}
}
items {
… on AllDiscountItems {
allItems
}
}
}
appliesOncePerCustomer
}
}
}
userErrors {
field
code
message
}
}
}
QUERY;

$variables = [
“basicCodeDiscount” => [
“title” => “20% Rajith Sahampathh”,
“code” => “SUMMER20”,
“startsAt” => “2022-06-21T00:00:00Z”,
“endsAt” => “2022-09-21T00:00:00Z”,
“customerSelection” => [
“customers” => [
81044372851254
],
],
“combinesWith” => [
“orderDiscounts” => true
],
“customerGets” => [
“value” => [
“percentage” => 0.2,
],
“items” => [
“all” => true,
],
],
“appliesOncePerCustomer” => true,
],
];

What is the issue of this. When add for all customers works fine.

Hi Rajith,

I was able to get something very similar to work with this mutation - you should be able to adjust it for your purpose:

mutation CreateDiscountCode {
  discountCodeBasicCreate(
    basicCodeDiscount: {
      title: "20% off all products",
      code: "DISCOUNT20",
      startsAt: "2023-10-01T00:00:00Z",
      endsAt: "2023-12-31T23:59:59Z",
      customerSelection: {
        customers: {
          add: ["gid://shopify/Customer/12345678"]
        }
      },
      customerGets: {
        value: {
          percentage: 0.2
        },
        items: {
          all: true
        }
      },
      appliesOncePerCustomer: true
    }
  ) {
    codeDiscountNode {
      codeDiscount {
        ... on DiscountCodeBasic {
          title
          codes(first: 10) {
            nodes {
              code
            }
          }
          startsAt
          endsAt
          customerSelection {
            ... on DiscountCustomers {
              customers {
                id
              }
            }
          }
          customerGets {
            value {
              ... on DiscountPercentage {
                percentage
              }
            }
            items {
              ... on AllDiscountItems {
                allItems
              }
            }
          }
          appliesOncePerCustomer
        }
      }
    }
    userErrors {
      field
      code
      message
    }
  }
}

You just need to replace gid://shopify/Customer/12345678 with gid://shopify/Customer/81044372851254 and it should work.

1 Like

Thanks a lot brother. It worked for me.

1 Like