Buy X Get Y

Overview

The Buy X Get Y function implements a classic promotional pattern: buy a certain quantity of items, and get other items discounted or free. The buy items and the get items can be the same products or entirely different products, which supports a wide range of promotional strategies.

PropertyValue
Discount ClassesProduct, Shipping
BillingStarter plan and above
Condition SupportAll 16 conditions. The 9 cart-level conditions decide whether the rule group applies. The 7 product-level conditions limit which lines count as buy items or get items. The shipping discount checks the 9 cart-level conditions and also needs enough matching items for at least one get item. Product-level conditions decide which lines count.

Buy X Get Y supports the Product and Shipping discount classes. The function counts qualifying “buy” items, calculates how many “get” items earn a discount, and by default gives that discount to the cheapest eligible get items first.

Key Concepts

ConceptDescription
buyQuantityThe number of items the customer must purchase to form one “set.”
getQuantityThe number of items that receive a discount per set.
maxSetsThe maximum number of sets the function will apply. Set to 0 for unlimited.
buyItemFilterDefines which products count as “buy” items. Uses filter types like all, collection, or productTag.
getItemFilterDefines which products are eligible as “get” items. Same filter types as buy.
Get item selection orderThe Get item selection order setting (itemSelectionOrder) decides which get items receive the discount: Cheapest items first (the default), Most expensive items first, or Cart order.
A unit counts onceWhen a product matches both filters, each unit counts as a buy item or as a get item, never as both. In a “Buy 2 Get 1 Free” offer where all products qualify, a cart of 3 identical items uses 2 as the buy items and 1 (the third) as the get item.

Free Gift with Purchase

A free gift with purchase is a Buy X Get Y rule group whose get discount is 100% off. For example, the buy filter is a serum collection with a buy quantity of 2, and the get filter is the lip balm product with a get quantity of 1.

Alfo does not add the free item to the cart. The shopper adds the get item to the cart like any other product. When the cart holds enough buy items and the get item, Alfo applies the get discount to the get item at checkout. Without enough buy items, the get item keeps its full price.

So tell shoppers which item to add, for example in the product description of the buy items. To make sure that only the gift gets the discount, set Customer gets from to Specific products or Specific variants and pick the gift.

Buy X Get Y configuration: Buy quantity 2, Get quantity 1, Max sets 0, and the buy items filter Specific products with 2 selected
Buy quantity 2, Get quantity 1, and the buy items: Specific products, 2 selected.
Get items filter Specific products with 1 selected, and the discount on get items: Percentage off 100 with the message Free lip balm with 2 serums
The get item: Specific products, 1 selected, at Percentage off 100.
Checkout line: Nourishing Lip Balm, $14.00 crossed out and FREE, with the message FREE LIP BALM WITH 2 SERUMS
At checkout, the lip balm shows $14.00 crossed out and FREE.

Real-Life Use Cases

1. Buy 2 Get 1 Free (Same Product)

A BOGO offer on any product. All products in the store qualify, and the cheapest item in the cart is the one discounted.

SettingValue
buyQuantity2
getQuantity1
buyItemFilterall
getItemFilterall
getDiscountPercentage, 100%
maxSets0 (unlimited)

A cart with 6 items gets 2 free items (2 sets of “Buy 2 Get 1”).

2. Buy 3 Coffees Get 1 Pastry 50% Off

A cross-category promotion where the buy and get items come from different product groups. This encourages customers to try a new category.

SettingValue
buyQuantity3
buyItemFilterCollection “Coffee”
getQuantity1
getItemFilterProduct tag “pastry”
getDiscountPercentage, 50%
maxSets1

3. Buy Laptop Get Case Free

A targeted accessory promotion tied to a high-value purchase. The buy filter targets a specific product or collection, and the get filter targets the accessory.

SettingValue
buyQuantity1
buyItemFilterCollection “Laptops”
getQuantity1
getItemFilterCollection “Laptop Cases”
getDiscountPercentage, 100%
maxSets1

4. Buy 2 Get 1 Free (Max 3 Sets)

Same as use case 1, but capped at 3 sets to limit exposure. A cart with 20 items still gets only 3 free items.

SettingValue
buyQuantity2
getQuantity1
maxSets3

Algorithm

The Buy X Get Y function follows this allocation algorithm. A unit counts once: as a buy item or as a free get item, never as both.

Step 1: Find the Eligible Lines

Gift cards never count. The product-level conditions of the rule group limit which lines count (see Rule Groups & Logic for AND and OR).

Step 2: Split the Units

Each unit of an eligible line falls into one group:

GroupUnits that match
Buy onlybuyItemFilter and not getItemFilter
Get onlygetItemFilter and not buyItemFilter
Bothboth filters

Step 3: Count the Free Units

s sets need s x buyQuantity buy units. The function takes them from the buy-only units first, then from the units that match both filters. The free units are the get-only units plus the units of both filters that the buy side leaves, up to s x getQuantity, so the last set can be partial:

free_units(s) = min(s * getQuantity, get_only + both_left_after_buy(s))

The function uses the number of sets that gives the most free units. When maxSets is above 0, it uses at most maxSets sets.

Step 4: Order the Get Lines

The Get item selection order setting orders the get lines: cheapest first (the default), most expensive first, or cart order.

Step 5: Allocate Discounts

Walk through the ordered get lines. Each line gives up to its quantity until the free units are used up. Lines that match both filters give, together, at most the units of both filters that the buy side left.

Walkthrough Example

Offer: Buy 2 Get 1 Free, all products, unlimited sets.

Cart contents:

ItemPriceQuantity
Socks$52
T-Shirt$203
Jacket$801

Step 1: All 3 lines are eligible.

Step 2: Every unit matches both filters: 0 buy-only units, 0 get-only units, 6 units of both filters.

Step 3: Count the free units for each number of sets:

SetsBuy unitsUnits leftFree units
124min(1, 4) = 1
242min(2, 2) = 2
360min(3, 0) = 0

2 sets give the most free units: 2.

Step 4: Cheapest first: Socks ($5), T-Shirt ($20), Jacket ($80).

Step 5: Socks: 2 units discounted. The 2 free units are used up. Stop.

Result: The 2 Socks receive the 100% discount (free), for $10 off. The 3 T-Shirts and the Jacket are the 4 buy items.

Configuration Example: Buy 2 Get 1 Free

{
  "version": "1.0",
  "strategy": "first",
  "ruleGroups": [
    {
      "id": "rg_001",
      "name": "Buy 2 Get 1 Free",
      "enabled": true,
      "conditionLogic": "and",
      "conditions": [],
      "buyQuantity": 2,
      "getQuantity": 1,
      "maxSets": 0,
      "buyItemFilter": { "filterType": "all" },
      "getItemFilter": { "filterType": "all" },
      "getDiscount": {
        "type": "percentage",
        "value": 100,
        "message": "Buy 2 Get 1 FREE"
      }
    }
  ],
  "rejectionRules": []
}

Key Fields

FieldPurpose
buyQuantityNumber of items required to form one buy set.
getQuantityNumber of items discounted per set.
maxSetsMaximum number of sets allowed. 0 means unlimited.
buyItemFilterFilter object determining which cart lines count as buy items.
getItemFilterFilter object determining which cart lines are eligible for the get discount.
getDiscountThe discount applied to get items. Supports percentage, fixedAmount, and fixedPrice (a fixed price per item) types.
filterTypeFilter mode: "all" (every product qualifies), "collection" (match by collection ID), "productTag" (match by product tag), "product" (specific products), or "productVariant" (specific variants).

Cross-Category Example

Buy 3 items from the Coffee collection, get 1 pastry-tagged item at 50% off, limited to 1 set per cart.

{
  "version": "1.0",
  "strategy": "first",
  "collectionIds": ["gid://shopify/Collection/coffee"],
  "productTags": ["pastry"],
  "ruleGroups": [
    {
      "id": "rg_001",
      "name": "Buy 3 Coffees Get 1 Pastry 50% Off",
      "enabled": true,
      "conditionLogic": "and",
      "conditions": [],
      "buyQuantity": 3,
      "getQuantity": 1,
      "maxSets": 1,
      "buyItemFilter": {
        "filterType": "collection",
        "collectionIds": ["gid://shopify/Collection/coffee"]
      },
      "getItemFilter": {
        "filterType": "productTag",
        "tags": ["pastry"]
      },
      "getDiscount": {
        "type": "percentage",
        "value": 50,
        "message": "Buy 3 Coffees, Get a Pastry 50% OFF"
      }
    }
  ],
  "rejectionRules": []
}

In this configuration, the collectionIds and productTags arrays at the top level tell the Shopify input query which collections and tags the function needs access to. The buyItemFilter and getItemFilter inside the rule group define the actual matching logic.

Conditional Buy X Get Y

You can add cart-level conditions to restrict who qualifies for the offer. For example, to limit a BOGO deal to customers tagged “member”:

{
  "conditions": [
    {
      "type": "customerTag",
      "operator": "hasAny",
      "tags": ["member"]
    }
  ]
}

Add this conditions array to the rule group alongside the buy/get configuration. Only customers with the “member” tag will see the discount applied.

Buy X Get Y Not Working: Checklist

  • Check that the get item is in the cart. Alfo discounts items that the shopper added to the cart. It never adds an item.
  • Count the units. A unit counts as a buy item or as a get item, never as both. “Buy 2 Get 1 Free” on one product needs 3 units of it.
  • Check the filters. The items must match Customer must buy from and Customer gets from. Gift cards never count as buy or get items.
  • Check the Product discount class. The get discount needs the Product class on the discount.
  • Check the conditions. Cart-level conditions decide whether the rule group applies. Product-level conditions limit which lines count.
  • Check a fixed price. With a fixed price, a get item that already costs that price or less gets no discount.
  • Check maxSets. When it is above 0, the cart gets at most that many sets.

For more checks, see BXGY Giving Too Many or Too Few Free Items and Discount Not Applying at Checkout on the Troubleshooting page.

Next Steps