shopify discount create

How to Create Product-Specific Discount Codes with discountCodeBasicCreate

I was looking for a way to create discount codes that apply only to specific products using the discountCodeBasicCreate mutation. Here’s the solution I found:

GraphQL Mutation

mutation CreateDiscountCode($basicCodeDiscount: DiscountCodeBasicInput!) {
  discountCodeBasicCreate(basicCodeDiscount: $basicCodeDiscount) {
    codeDiscountNode {
      id
      codeDiscount {
        ... on DiscountCodeBasic {
          title
          codes(first: 5) {
            nodes {
              code
            }
          }
          startsAt
          endsAt
          status
          usageLimit
          appliesOncePerCustomer
          customerGets {
            value {
              ... on DiscountPercentage {
                percentage
              }
              ... on DiscountAmount {
                amount {
                  amount
                  currencyCode
                }
              }
            }
            items {
              ... on DiscountProducts {
                products(first: 10) {
                  nodes {
                    id
                    title
                  }
                }
              }
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Variables for Specific Products

{
  "basicCodeDiscount": {
    "title": "20% off selected products",
    "code": "PRODUCT20",
    "startsAt": "2025-07-31T00:00:00Z",
    "endsAt": "2025-12-31T23:59:59Z",
    "customerSelection": {
      "all": true
    },
    "customerGets": {
      "value": {
        "percentage": 0.2
      },
      "items": {
        "products": {
          "productsToAdd": [
            "gid://shopify/Product/YOUR_PRODUCT_ID_1",
            "gid://shopify/Product/YOUR_PRODUCT_ID_2"
          ]
        }
      }
    },
    "minimumRequirement": {
      "subtotal": {
        "greaterThanOrEqualToSubtotal": "25.0"
      }
    },
    "usageLimit": 200,
    "appliesOncePerCustomer": false
  }
}

Alternative: Target Specific Product Variants

{
  "basicCodeDiscount": {
    "title": "$10 off specific variants",
    "code": "VARIANT10",
    "startsAt": "2025-07-31T00:00:00Z",
    "endsAt": "2025-12-31T23:59:59Z",
    "customerSelection": {
      "all": true
    },
    "customerGets": {
      "value": {
        "discountAmount": {
          "amount": "10.0",
          "appliesOnEachItem": true
        }
      },
      "items": {
        "productVariants": {
          "productVariantsToAdd": [
            "gid://shopify/ProductVariant/YOUR_VARIANT_ID_1",
            "gid://shopify/ProductVariant/YOUR_VARIANT_ID_2"
          ]
        }
      }
    },
    "usageLimit": 50,
    "appliesOncePerCustomer": true
  }
}

Key Points:

  1. Product targeting is done through the customerGets.items.products.productsToAdd field
  2. Variant targeting uses customerGets.items.productVariants.productVariantsToAdd
  3. Collection targeting is possible with customerGets.items.collections.collectionsToAdd
  4. Product IDs must be in GID format: gid://shopify/Product/123456789
  5. Percentage values use decimal format (0.2 = 20%)
  6. Fixed amounts use string format (“10.0” for $10)

:warning: Important Limitation:

You can only create ONE discount type at a time per mutation. This means:

  • You can target multiple products in one mutation
  • OR you can target multiple variants in one mutation
  • OR you can target multiple collections in one mutation
  • But you CANNOT mix products + variants + collections in the same mutation

If you need to target both specific products AND collections, you’ll need to create separate discount codes with separate mutations.

Example: Collection-Based Discount (Separate Mutation)

{
  "basicCodeDiscount": {
    "title": "15% off summer collection",
    "code": "SUMMER15",
    "startsAt": "2025-07-31T00:00:00Z",
    "endsAt": "2025-09-30T23:59:59Z",
    "customerSelection": {
      "all": true
    },
    "customerGets": {
      "value": {
        "percentage": 0.15
      },
      "items": {
        "collections": {
          "collectionsToAdd": [
            "gid://shopify/Collection/YOUR_COLLECTION_ID"
          ]
        }
      }
    },
    "minimumRequirement": {
      "subtotal": {
        "greaterThanOrEqualToSubtotal": "75.0"
      }
    },
    "usageLimit": null,
    "appliesOncePerCustomer": false
  }
}

Product Targeting Options:

  • Specific Products: "products": {"productsToAdd": [...]} - Multiple products in one mutation
  • Specific Variants: "productVariants": {"productVariantsToAdd": [...]} - Multiple variants in one mutation
  • Collections: "collections": {"collectionsToAdd": [...]} - Multiple collections in one mutation
  • All Products: "all": true

Remember: Each mutation can only use ONE of these targeting methods at a time!

This approach allows you to create targeted discount codes without needing the deprecated priceRuleCreate mutation.

Hope this helps others looking for the same functionality!

2 Likes

Thanks for this @ASWIN_S ! Appreciate you taking the time to document this for the community!

1 Like

This is awesome @ASWIN_S - thank you for posting this!!

1 Like