# Buy X, get Y discounted on Shopify, with free shipping on the same code

To run "buy X, get Y" with [OneCode: Stackable Discounts](https://apps.shopify.com/rm-combo-discounts), add a **Product Discount** that targets the Y items, with the Per-Discount Condition **Cart Contains** set to the X product. Y is discounted only while X is in the cart. Add a **Quantity Limit** of `1` so only one Y item gets the discount. X can be a product or any item from a collection, and so can Y. The same code can also give free shipping, or hold a second "buy X, get Y" offer.

## Example

The code BEANIEBONUS: buy a Merino Beanie, get one item from Accessories 20% off.

- A $32 beanie and a $30 canvas tote from Accessories: the tote is $24, the beanie stays $32. Subtotal $56.
- A $32 beanie, a $30 tote and a $45 scarf from Accessories: with **Cheapest items first**, the tote is $24 and the scarf stays $45. Subtotal $101. With **Most expensive items first**, the scarf is $36 and the tote stays $30. Subtotal $98.
- A $30 tote on its own: no beanie, so no discount.

![BEANIEBONUS at checkout. The $32 Merino beanie is full price, the $30 canvas tote is $24, the subtotal is $56, shipping is $8 and the total is USD $64.00.](/images/checkout-beanie.png)

The checkout above is the first cart, before any shipping discount is added.

## Set it up

1. In OneCode, click **Create discount**. Choose **Discount code** and enter `BEANIEBONUS`, or choose **Automatic discount** and give it a **Title**.
2. Click **Add Discount Type**. Keep **Product Discount** and **Percentage**, and enter `20` in **Percentage**.
3. Set **Target** to **Specific Collections**, click **Browse** and pick Accessories. These are the Y items.
4. Click **Add quantity limit**. Enter `1` in **Quantity Limit** and choose **Cheapest items first** or **Most expensive items first** in **Apply discount to**.
5. Under **Per-Discount Conditions**, click **Add Condition**. Set **Field** to **Cart Contains**, keep **Contains** and **Specific Products**, click **Browse** and pick the Merino Beanie. This is X.
6. Click **Save discount**.

For "buy X, get Y free", enter `100` instead of `20`. The customer adds Y to the cart. OneCode discounts it; it doesn't add it for them.

## Add free shipping to the same code

1. Click **Add Discount Type** and set **Discount Type** to **Shipping Discount**. Keep **Free Shipping**.
2. Under its **Per-Discount Conditions**, click **Add Condition**, pick **Match Another Discount's Rules**, and tick the 20% Accessories discount.

Shipping is now free only when the "buy X, get Y" offer applies. Without that condition, the shipping discount has no rules of its own, so every order with the code would ship free.

![The OneCode editor for the code BEANIEBONUS: a 20% Product Discount on the Accessories collection with a Quantity Limit of 1, Cheapest items first, and the Per-Discount Condition Cart Contains the Merino Beanie, plus a Free Shipping discount on all rates that matches the other discount's rules](/images/editor-buy-x-get-y.png)

With the shipping discount added, the first cart from the example pays $56:

![BEANIEBONUS with free shipping at checkout. The $32 Merino beanie is full price, the $30 canvas tote is $24, Economy shipping is free instead of $8, and the total is USD $56.00.](/images/checkout-beanie-free.png)

Shopify has its own [Buy X Get Y discount type](https://help.shopify.com/en/manual/discounts/discount-types), and for a plain "buy this, get that" it does the job. It only discounts the item the customer gets, though, so free shipping or money off other items would need separate discounts. With OneCode they go on one code.

## More than one offer on one code

Because **Cart Contains** sits on the discount type, not on the whole code, one code can hold several offers. Add a second **Product Discount**, for example 10% off Mugs with **Cart Contains** the Coffee collection. Each offer applies only when its own X is in the cart. If an item qualifies for two offers, it gets the one that saves more, not both.

## Things to check before you launch

- If X is also in the Y collection, it can get the discount too. Here, if the beanie were in Accessories, it would count as a Y item. To keep it out, set **Target** to **Specific Products** and pick the Y products instead of the collection.
- **Cart Contains** checks that X is in the cart, not how many. One beanie is enough. For "buy 2 from a collection, get the cheapest free", see [BOGO](/guides/bogo.md).
- To trigger the offer with any item from a collection, choose **Specific Collections** under **Items** in the condition.
- The quantity limit counts per order. Two beanies still get one discounted accessory with a limit of `1`.
- For a free gift on a spend threshold rather than a product, see [free gift with purchase](/guides/free-gift-with-purchase.md). To discount a whole collection when a product is in the cart, see [discount when a product is in the cart](/guides/discount-when-product-in-cart.md).

OneCode costs $9.90 USD every 30 days after a 7-day free trial, billed by Shopify. Install it from the [Shopify App Store](https://apps.shopify.com/rm-combo-discounts).
