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 the Buy X Get Y limitation.

Written by Tom

The IF Discount Code 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 gift when a shopper applies a promo code in the cart. This rule reads the codes the storefront reports as applied and accepted, 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 IF Discount Code Rule changes what a widget does based on which valid discount code is applied to the cart. It reacts to a code that Shopify has already accepted, so its purpose is to read the current state of the cart and adjust the widget's output, not to create or complete a discount. Use it whenever you want a widget to behave differently because a specific code is present.

The rule supports three main uses:

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 an independently valid discount code. When the applied code is a standalone 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.

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.

In every case, the code must already be valid in Shopify. The IF Discount Code Rule reacts to a discount that is complete, so it cannot be used to satisfy a code's own conditions, such as adding the product a Buy X Get Y code requires. For that limitation and what to use instead, see the Buy X Get Y section below.


IF Discount Code Rule Operators

The IF Discount Code Rule supports four operators that 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 valid applied code matches the value.

Screenshot example above of the IF 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 an IF Discount Code Rule

Set up an IF Discount Code Rule inside 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 IF Discount Code Rule requires the discount code to be valid in Shopify at the time it is applied. Code validation happens on the storefront, and Shopify is the source of truth for whether a code is valid. The rule fires only for a code that Shopify accepts. A code that Shopify rejects as invalid or expired shows an error in the discount field and does not fire the rule.

This means the code must exist and be active in Shopify, and it must pass validation at the moment the shopper enters it in the cart. A code that has not been created in Shopify, or that Shopify will not accept in the current cart, will not trigger the rule.


Why a Buy X Get Y Code Cannot Trigger the Product It Adds

A Buy X Get Y discount code cannot be used as the trigger for the same product the IF Discount Code Rule is set to add. This is the most common reason a discount-code-triggered gift fails to appear, and it comes from how Shopify validates Buy X Get Y codes combined with how the rule validates codes.

The IF Discount Code Rule fires on its own, and only after Shopify has already accepted and validated the applied code. This timing is the key to understanding how it behaves with a Buy X Get Y code.

A Buy X Get Y code is not accepted until its conditions are already met, which means its Get Y product is already in the cart. Because the Rebuy IF Discount Code Rule fires only after Shopify validates the code, the rule runs at a point where the Get Y product is already present. This is why the rule cannot add that Get Y product itself: the code the rule depends on will not validate until the product is in the cart, so the rule can only fire after the product has been added some other way.

You can still use a Buy X Get Y code with the IF Discount Code Rule. The rule will read the code and fire once Shopify validates it. It simply cannot be the mechanism that adds the code's own Get Y product

There is no way to make a Buy X Get Y code add its own Get Y product through the IF Discount Code Rule. If the goal is to add a gift automatically, use one of these approaches instead, none of which depend on a code adding the product it is scoped to:

  • Trigger the gift with a code that is valid in Shopify independently of the gift product. The code must not be a Buy X Get Y, and must not be scoped to the gift. The IF Discount Code Rule adds the gift, and the widget's own discount setting makes it free.

  • Skip the discount code entirely and trigger the gift with another cart-based rule, such as IF Cart Subtotal or IF Cart Contains, then let the widget's discount setting handle the free pricing.

  • Use the Tiered Progress Bar to unlock the gift at a spend threshold. The Tiered Progress Bar adds the gift and applies its discount through Shopify Functions when the cart reaches the threshold, with no discount code involved.

The takeaway: the widget adds and discounts the gift, and the code, if you use one, is only the flag that tells Rebuy to add it. Never have the Shopify code and the widget both discount the same product, and never use a Buy X Get Y code as the trigger for the product it gates.


IF Discount Code Rule Limitations

Codes that are only valid once the output product is in the cart will not fire the rule. Because Shopify must accept the code at the moment it is applied, a discount code whose own validity depends on the rule's RETURN product already being in the cart, such as a Buy X Get Y or amount-off-products code scoped to that product, is rejected before the rule can add the product. For a code-gated auto-add, use a code that is valid independently of the RETURN product, or apply the product's discount through the widget's Shopify Functions setting rather than a code that depends on that product being present. See section Why a Buy X Get Y Code Cannot Trigger the Product It Adds.

Automatic discounts are not evaluated. The IF Discount Code Rule evaluates only manually entered discount codes. Automatically applied discounts, such as Buy X Get Y offers and automatic percentage-off discounts, are not captured by this rule, because no code is entered for the cart to report.

Post Purchase Offer widgets are not supported. The IF Discount Code Rule cannot be used on Post Purchase Offer widgets.


Which Widgets Support the IF Discount Code Rule

The IF Discount Code 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 IF Discount Code 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 and accepted.


Frequently Asked Questions About the IF Discount Code Rule

Does the IF Discount Code Rule require the discount code to be valid in Shopify?

The IF Discount Code Rule requires the discount code to be valid in Shopify at the time it is applied. Code validation happens on the storefront and Shopify is the source of truth for whether a code is valid, so the rule fires only for a code that Shopify accepts. If a shopper enters a code that Shopify rejects as invalid or expired, the discount field shows an error and the rule does not fire.

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

A discount code can trigger a Gift With Purchase (GWP) in Rebuy by pairing a GWP widget with the IF Discount Code 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 code must be valid in Shopify independently of the gift, so it cannot be a Buy X Get Y code scoped to the gift product, because that code will not validate until the gift is already in the cart. The rule reads only manually entered codes, and it is not supported on Post Purchase Offer widgets.

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

A discount-code-triggered product usually fails to add because the code is a Buy X Get Y whose Get Y is the same product the IF Discount Code Rule is set to return. The rule reads the code only after Shopify accepts it, and Shopify will not accept a Buy X Get Y code until its Get Y product is already in the cart, so the rule cannot be the thing that adds that product.

Does the IF Discount Code Rule work with automatic discounts?

The IF Discount Code Rule evaluates only manually entered discount codes. Automatic discounts, such as Buy X Get Y offers and automatic percentage-off discounts, are not captured by the rule, because no code is entered for the cart to report.

Can a Buy X Get Y code add its own Get Y product through the IF Discount Code Rule?

The IF Discount Code Rule can read a Buy X Get Y code, but it cannot add that code's own Get Y product, because Shopify will not accept the code until the Get Y product is already in the cart and the rule fires only after Shopify accepts the code. Using a Buy X Get Y code to add its own Get Y product is not a supported pattern, Use a different method for a gift you want added automatically. Instead use an independently valid code, a cart-based rule, or the Tiered Progress Bar when you need the gift added automatically.

Did this answer your question?