Creating order with Buy X Get Y discount

Hi Shopify Support,

I need to create orders programmatically with Buy X Get Y (BXGY) discount codes using the GraphQL Admin API.

Questions:

  1. Does the orderCreate mutation support BXGY discount codes natively?

  2. If yes, what is the correct input syntax to apply BXGY discounts?

  3. If no, what is the recommended approach for programmatically creating orders with BXGY discounts?

Current Issue:

I’m using the orderCreate mutation to create cash orders. When I try to apply a BXGY discount, it doesn’t evaluate the “Buy X to unlock Get Y” logic correctly.

My Current Code (createCashOrder.ts):

TypeScript
// GraphQL mutation I'm using

const orderCreateMutation = `

mutation orderCreate($order: OrderCreateOrderInput!) {

**orderCreate(order: $order) {**

  **order {**

    **id**

    **name**

    **totalPriceSet {**

      **shopMoney {**

        **amount**

        **currencyCode**

      **}**

    **}**

    **lineItems(first: 50) {**

      **edges {**

        **node {**

          **id**

          **title**

          **quantity**

          **originalUnitPriceSet {**

            **shopMoney {**

              **amount**

              **currencyCode**

            **}**

          **}**

          **discountAllocations {**

            **allocatedAmountSet {**

              **shopMoney {**

                **amount**

                **currencyCode**

              **}**

            **}**

          **}**

        **}**

      **}**

    **}**

  **}**

  **userErrors {**

    **field**

    **message**

  **}**

**}**

}

`;

// Line items input

const lineItemsInput = lineItemsArray.map((item: any) => ({

variantId: `gid://shopify/ProductVariant/${item.variantId}`,

quantity: parseInt(item.quantity) || 1,

priceSet: {

**shopMoney: {**

  **amount: priceWithTax.*toFixed*(2),**

  **currencyCode: 'INR'**

}

}

}));

// Order input with BXGY discount attempt

const orderInput = {

lineItems: lineItemsInput,

shippingAddress: shippingAddressInput,

billingAddress: billingAddressInput,

// :cross_mark: PROBLEM: How do I apply BXGY discount here?

// I only see these options:

itemPercentageDiscountCode: {

**code: "BUYXGETY50",**

**percentage: 50  *// But this applies to ALL items, not just "Get Y" items***

}

// :cross_mark: These don’t exist:

// bxgyDiscountCode: ???

// buyXgetYDiscountCode: ???

};

const response = await shopify.graphql(orderCreateMutation, { order: orderInput });

What Happens:

  • If I use itemPercentageDiscountCode, it applies 50% to ALL line items (wrong!)

  • I cannot find a way to tell Shopify: “Apply 50% discount ONLY to Powder item, ONLY IF Fresh item is purchased”

Example BXGY Discount:

  • Code: BUYXGETY50

  • Type: Buy X Get Y

  • Customer Buys: 1 quantity of “Fresh” product

  • Customer Gets: 50% off “Powder” product

This works correctly on the storefront checkout, but I need to replicate this in my backend POS system.

Workaround I’m Considering:

Should I manually calculate the BXGY discount in my application and send pre-discounted prices in the lineItems.priceSet? Or is there a proper way to apply BXGY discount codes via the API?

API Version: 2025-07

Thank you for your help!

Best regards,
Vedant Naik

Hey @Vedant_Naik1 :waving_hand: - right, now, orderCreate doesn’t support discounts that only apply to one line item, there’s a bit more info here:

There are a few workarounds that folks do use, and your idea about manually calculating the discount and then changing the line item price itself is a decent one (you could also add an order tag to the order if needed, in case you wanted to track which orders are intended to be discounted). Here’s how I’d do this:

Fetch the BXGY discount configuration using the GraphQL API:

query {
  codeDiscountNodeByCode(code: "BUYXGETY50") {
    id
    codeDiscount {
      ... on DiscountCodeBxgy {
        customerBuys {
          items { ... }
          value { ... }
        }
        customerGets {
          items { ... }
          value { ... }
        }
      }
    }
  }
}

  • Then, in your app, implement the BXGY logic instead of the discount code to check it against the desired discount. For example:
    • Check if “Fresh” product is in cart with required quantity
    • If yes, calculate 50% discount for “Powder” product
    • Pass pre-discounted prices in lineItems[].priceSet when calling orderCreate

I tested this just to make sure this would work and it should provided your integration has the right order/product access scopes:

mutation orderCreate($order: OrderCreateOrderInput!) {
  orderCreate(order: $order) {
    order {
      id
      name
      totalPriceSet {
        shopMoney {
          amount
          currencyCode
        }
      }
      lineItems(first: 10) {
        edges {
          node {
            id
            title
            quantity
            variant {
              id
            }
            originalUnitPriceSet {
              shopMoney {
                amount
                currencyCode
              }
            }
          }
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

Variables:

{
  "order": {
    "lineItems": [
      {
        "variantId": "gid://shopify/ProductVariant/variant-id-here",
        "quantity": 1,
        "priceSet": {
          "shopMoney": {
            "amount": "5.00",
            "currencyCode": "USD"
          }
        }
      }
    ],
    "email": "test@example.com"
  }
}

Hope this helps, let me know if I can clarify anything on our end here.

1 Like

Hey @Vedant_Naik1 - just wondering if the above helped, let me know! :slight_smile:

1 Like

yes Alan_G it worked thanks alot

Hey @Vedant_Naik1 - glad this helped, let me know if I can assist further!