Skip to main content

Discount Codes as a Condition Input for Data Sources

Use a discount code as a Data Source condition to trigger a Rebuy widget. Covers operators, Shopify code validation, triggering a Gift With Purchase, and running a Buy One Get One offer from a Buy X Get Y code.

Written by Tom

The Discount Code data source rule is a cart-based Data Source rule that fires when a shopper has a specific discount code applied to their cart. Use it to change what a Rebuy widget returns based on the code entered, such as adding specific product recommendations or a gift when a shopper applies a promo code in the cart. This rule reads the codes the storefront reports for the cart, including codes entered in the Smart Cart discount field.

For the full Data Source rules reference covering every IF and RETURN type, see Data Sources 101. For the other cart-based rules, see Data Sources And The Cart.


Discount Code Rule Use Cases

The Discount Code data source rule changes what a widget does based on which discount code is applied to the cart. Use it whenever you want a widget to behave differently because a specific code is present.

The rule supports the uses described below.

Target recommendations by discount code. When a shopper applies a campaign or VIP code, a cart-based widget re-evaluates and returns a product set matched to that code with Equals or Contains. The reverse also works: Does Not Contain or Does Not Equal can hide or swap an offer when a particular code is present, so you can suppress an upsell for shoppers who already have a specific promotion applied.

Add a bonus item on top of a standalone discount code. When the applied code is a Shopify discount that a shopper receives anyway, such as an order percentage, a shipping discount, or a campaign code, its presence can flag the widget to add a bonus product. The code discounts the order, and the widget's own discount setting discounts the bonus. These are two separate jobs, handled by two separate tools.

Run a Buy One Get One offer from a Buy X Get Y code. A Gift With Purchase widget adds the reward product the moment the shopper enters the code, so the cart satisfies the code's conditions and Shopify applies the discount. See Use the IF Discount Code Rule for a Buy One Get One (BOGO) Offer below.

Segment offers for campaigns and partners. An influencer or wholesale code can both apply its own discount and unlock a curated widget offer keyed to that code, so different audiences see different products when they apply their code.


Discount Code data source rule operators

The Discount Code data source rule supports the operators listed below, which control how the applied code is matched. The value you enter is a discount code, or part of a code.

  • Contains matches a partial code, so a value of SUMMER fires for an applied code of SUMMER25.

  • Does Not Contain fires when no applied code contains the value.

  • Equals matches the full code exactly.

  • Does Not Equal fires when no applied code exactly matches the value.

Matching is not case-sensitive, so SUMMER and summer are treated the same. If more than one code is applied to the cart, the IF Discount Code Rule fires when any applied code matches the value.

Screenshot example above of the Discount Code Rule in the Rebuy data source rule builder, with the Object set to Discount Code and the Operator dropdown showing Contains, Does Not Contain, Equals, and Does Not Equal.


How to set up a Discount Code data source rule

Set up an Discount Code data source rule the data source of any cart-based widget.

  1. Open the widget and go to its Data Source.

  2. Add a rule and set the IF object to Discount Code.

  3. Choose an operator: "Equals" to match a full code, or "Contains" to match part of a code. The reverse also works: "Does Not Contain" or "Does Not Equal" - This lets you suppress or change an offer for shoppers who already have a specific promotion applied.

  4. Enter the discount code, or the portion of it you want to match, in the value field.

  5. Set the RETURN to the product or content you want to surface when the code is applied, such as Specific Product for a gift.

  6. Save the widget.

Example:

IF  Discount Code Contains → "VIP"
RETURN Specific Product → [VIP Gift Product]

This rule returns the VIP gift product whenever an applied code contains VIP.

Screenshot example above of a configured IF Discount Code Rule set to Discount Code Contains "VIP" on the IF side and RETURN Specific Product set to a gift product on the return side.


How the IF Discount Code Rule Validates Codes in Shopify

The Discount Code data source rule requires the discount code to exist and be active in Shopify. Shopify is the source of truth for the code, and the rule fires only for a code the shop actually has. A code that has never been created in Shopify, or that is expired or deactivated, shows an error in the discount field and does not fire the rule.

Being active is not the same as being applicable to the cart in front of the shopper. On a Gift With Purchase widget, an active code whose conditions the cart does not yet satisfy, such as a Buy X Get Y code whose reward product has not been added, still fires the rule, so the widget can add the product that completes the code. Shopify then applies the discount once the cart qualifies.


Use the Discount Code data source rule for a Buy X Get Y and BOGO offer

A Buy One Get One offer, which Shopify creates as a Buy X Get Y discount code, runs in Rebuy by pairing a Gift With Purchase widget with the Discount Code data source rule. The shopper adds only the qualifying product, enters the code, and Rebuy adds the reward product so the code's conditions are met and Shopify discounts it. Gift With Purchase widgets do this on their own, with nothing to enable.

Build this on a Gift With Purchase widget. It is the widget type that adds the reward product before Shopify has applied the code, which is what makes the offer work from a half-filled cart. The Discount Code data source rule on another cart widget waits for Shopify to apply the code first, so it never gets the chance to add the product that would make the code apply.

How the BOGO Offer Works

A BOGO offer works because the Gift With Purchase widget adds the free item as soon as the shopper enters the Buy X Get Y code, before Shopify has applied that code to the cart.

The example setup offer below is what the shopper experiences on a "buy 1 t-shirt, get 1 t-shirt free" offer.

  1. The shopper adds the qualifying t-shirt to the cart. Only the Buy X side of the offer is present.

  2. The shopper enters the Buy X Get Y code in the Smart Cart discount field.

  3. The Gift With Purchase widget's Data Source evaluates, the Discount Code data source rule matches the code, and the widget adds the reward t-shirt returned by the rule.

  4. The cart now satisfies the code's conditions, so Shopify applies the Buy X Get Y discount and the reward t-shirt becomes free.

Without the widget, the shopper would have to add both t-shirts themselves before Shopify would take the code.

Set up a BOGO offer triggered by a discount code

Set up a BOGO offer with a Shopify Buy X Get Y discount code and a Rebuy Gift With Purchase widget, using the steps below.

  1. In Shopify, create the Buy X Get Y discount code and confirm it is active. Set the Get Y product to the reward you want the shopper to receive.

  2. In Rebuy, open or create a Gift With Purchase widget.

  3. In the widget's Data Source, add a rule with the IF object set to Discount Code. Choose Equals to match the full code, or Contains to match a family of codes that share a prefix.

  4. Enter the discount code in the value field.

  5. Set the RETURN to Specific Product and select the same product the Shopify discount uses as its Get Y.

  6. Open the widget's Discount screen and leave the Shopify Functions discount set to None. The Shopify Buy X Get Y code is what discounts the reward product.

  7. Save the widget.

Do not discount the reward product twice. When a Shopify Buy X Get Y code discounts the reward product, leave the Gift With Purchase widget's Shopify Functions discount set to None. Two product discounts targeting the same line item is a Shopify combination conflict, and Shopify applies one and ignores the other rather than stacking them.

Discount Code data source rule BOGO setup: the Gift With Purchase widget's Discount screen with the Shopify Functions discount set to None, which is the correct setting when a Shopify Buy X Get Y code is discounting the reward product.

Adding the reward product and discounting it are separate jobs. The Gift With Purchase widget adds the reward product, and the Shopify Buy X Get Y code discounts it. If the reward product is missing from the cart, the problem is in the Data Source rule. If the reward product is in the cart at full price, the problem is in the Shopify discount, and the fix is not to turn on the widget's Shopify Functions discount, because that competes with the code for the same line item.

Example Data Source configuration and images below:

IF  Discount Code Equals → "BuyXGetY"
RETURN Specific Product → [Shirt]

Discount Code data source rule BOGO setup: the Shopify discount BuyXGetY, type Buy X get Y product discount, with Customer buys set to minimum quantity 1 of Specific products Shirt, Customer gets set to quantity 1 of Specific products Shirt, At a discounted value set to Free, the code active and combining with product and order discounts.

Discount Code data source rule BOGO setup: Rule #1 in the Rebuy Data Source rule builder with IF set to Discount Code, operator Equals, value BuyXGetY, and RETURN set to Specific Product Shirt, the same product the Shopify Buy X Get Y discount uses as its Get Y.

Discount Code data source rule BOGO result: the Rebuy Smart Cart after the shopper entered the BUYXGETY code with one Shirt in the cart, showing the reward Shirt added by the Gift With Purchase widget at $0.00


Discount Code data source rule Limitations

Automatic discounts are not evaluated. The Discount Code data source rule evaluates only discount codes the shopper enters. A Shopify discount configured to apply automatically, including an automatic Buy X Get Y discount that needs no code, is not captured by this rule, because no code is entered for the cart to report. A Buy X Get Y discount the shopper enters as a code does fire the rule.

Post Purchase Offer widgets are not supported. The Discount Code data source rule cannot be used on Post Purchase Offer widgets.


Which Widgets Support the Discount Code data source rule

The Discount Code data source rule is available for Checkout Extensions and onsite cart-based widgets, including the Smart Cart, collection page, homepage, and cart page widgets. It is not supported on Post Purchase Offer widgets.

Product Page widgets can use the Discount Code data source rule, but they will not respond the moment a shopper applies a code. A Product Page widget evaluates its rules on page load, so it does not re-check the cart when a discount code is entered afterward. The widget picks up the applied code only the next time the page loads, meaning the shopper would need to refresh or navigate to another page before the rule fires. For a response the moment a code is applied, use a cart-based widget such as the Smart Cart, where the rule evaluates as soon as the code is entered.


Frequently Asked Questions About the Discount Code data source rule

What makes a discount code eligible to fire the Discount Code data source rule?

The Discount Code data source rule fires for a discount code that exists and is active in Shopify and that the shopper enters in the cart. A code that has never been created in Shopify, or that is expired or deactivated, shows an error in the discount field and does not fire the rule. On a Gift With Purchase widget, an active code whose conditions the cart does not yet satisfy still fires the rule, so the widget can add the product that completes the code.

Can a discount code trigger a Gift With Purchase (GWP) in Rebuy?

Yes, a discount code can trigger a Gift With Purchase (GWP) in Rebuy by pairing a GWP widget with the Discount Code data source rule. Set the IF rule to Discount Code, choose Equals or Contains, enter the code, and set the RETURN to a Specific Product. When the shopper applies the code in the Smart Cart discount field, the rule fires and the gift is added to the cart. The rule reads only manually entered codes, and it is not supported on Post Purchase Offer widgets.

Can a Buy X Get Y code add its own Get Y product through the Discount Code data source rule?

Yes, a Buy X Get Y code can add its own Get Y product through the Discount Code data source rule when the rule is on a Gift With Purchase widget. The shopper enters the code with only the qualifying product in the cart, the widget adds the Get Y product returned by the rule, and Shopify applies the Buy X Get Y discount once the cart satisfies the code. Set the widget's RETURN to the same product the Shopify discount uses as its Get Y, and leave the widget's own Shopify Functions discount set to None so the code is the only thing discounting that product.

Why is my discount-code-triggered product not being added to the cart?

A discount-code-triggered product usually fails to add for one of the reasons below.

  • The code does not exist in Shopify, or it is expired or deactivated. Confirm the code is active in Shopify Admin.

  • The rule value does not match the code the shopper typed. Equals requires the full code, so use Contains to match part of a code.

  • The rule is on a Product Page widget, which evaluates on page load and does not re-check the cart when a code is entered afterward. Move the rule to a cart-based widget.

  • The rule is on a Post Purchase Offer widget, which does not support the IF Discount Code Rule.

  • The discount is set to apply automatically in Shopify rather than by code, so no code is entered for the cart to report.

Why won't my Buy X Get Y code trigger the free gift?

A Buy X Get Y code that does not trigger the free gift is usually one of two different problems, and they have opposite fixes. If the reward product never appears in the cart at all, work through the causes below. If the reward product appears but the shopper is still charged for it, that is a discounting problem, not a triggering one.

When the reward product is never added:

  • The code does not exist in Shopify, or it is expired or deactivated. Confirm the code is Active in Shopify Admin under Discounts.

  • The rule is on a widget other than a Gift With Purchase widget. Only a Gift With Purchase widget adds the reward product before Shopify has applied the code.

  • The rule value does not match the code the shopper typed. Equals requires the full code, so use Contains to match part of a code.

  • The RETURN is not set to the product the Shopify discount uses as its Get Y, so the widget adds something the code cannot discount.

  • The discount is set to apply automatically in Shopify rather than by code, so no code is entered for the cart to report.

  • The rule is on a Product Page widget, which evaluates on page load and does not re-check the cart when a code is entered afterward. Move the rule to a cart-based widget.

  • The rule is on a Post Purchase Offer widget, which does not support the Discount Code data source rule.

Turning on Discounted By Functions does not fix a reward product that is never added, and on a Buy X Get Y offer it creates a second problem, because the Shopify code is already discounting that product.

Does the Discount Code data source rule work with automatic discounts?

No, the Discount Code data source rule does not work with automatic discounts, because it evaluates only discount codes the shopper enters and an automatic discount produces no code for the cart to report. This includes an automatic Buy X Get Y discount. The same Buy X Get Y offer distributed as a code that shoppers type in does fire the rule.

Can I enter multiple discount codes separated by commas in one Discount Code data source rule?

You cannot enter multiple discount codes separated by commas in one Discount Code data source rule, because the rule treats the entire value as a single code and does not split it on commas. A value of SUMMER,VIP is matched as the literal text "SUMMER,VIP", which no real applied code will equal or contain, so the rule never fires. To respond to more than one code, use one of these approaches:

  • Use the Contains operator with a shared prefix to match a family of codes in one rule. A value of SUMMER matches SUMMER25, SUMMER50, and SUMMERVIP.

  • Add a separate Discount Code data source rule for each code, each in its own rule block. Separate rule blocks are evaluated as OR, so the widget responds when any one of the codes is applied.

Matching is not case-sensitive, and the rule reads every code applied to the cart, so it fires when any applied code matches the value.

If a shopper removes the discount code at checkout, is the gift removed?

A gift added by an Discount Code data source rule is removed at checkout when the shopper removes the code, but only on Shopify Plus stores that have a Rebuy checkout widget active. Rebuy re-runs the Gift With Purchase data source rules during Shopify checkout, so when the applied code is removed and the rule no longer matches, the gift no longer qualifies and Rebuy removes it from the checkout. This checkout validation only removes gifts that no longer qualify; it does not add new gifts at checkout, because gifts are added by the Gift With Purchase widget on storefront pages such as the Smart Cart.

For the removal to run at checkout, all of the following must be true:

  • The store is on Shopify Plus, which is required for the checkout extensions this feature uses.

  • At least one Rebuy checkout widget, a checkout recommendation, checkout progress bar, or checkout content block, is active on the checkout page.

  • The data source contains no rule types that checkout extensions cannot evaluate, such as URL rules or order tag rules. If it does, the checkout validation is skipped for that entire data source and the gift stays in the cart.

On the storefront, the gift is managed separately: the Gift With Purchase widget in the Smart Cart adds and removes the gift as the cart changes. For full details on checkout validation, see Creating a Gift With Purchase Offer.

Did this answer your question?