Compare At Price (Sale Fallback)
Overviewโ
Generic sale discount available to all shoppers (guests and customers). Acts as a fallback when no price match is found.
Eligibilityโ
Everyone - No customer tags required
When It Appliesโ
- No price match was found for the line item
- Product variant has a
compare_at_priceset in Shopify
How It Worksโ
The product variant's regular price is replaced with the compare_at_price value.
Example:
- Regular price: $100
- Compare at price: $80
- Result: Customer pays $80
Discount Messageโ
Customers see: "Sale! X% off!"
Where X is the percentage discount calculated from the price difference.
Key Differences from Price Matchingโ
| Feature | Compare At Price | Price Matching |
|---|---|---|
| Eligibility | Everyone | Tagged customers only |
| Message | "Sale! X% off!" | Campaign-specific message |
| Personalization Surcharge | โ No | โ Yes (+$6) |
| Priority | Lower | Higher |
| Setup | Set in Shopify variant | Customer + product tags |
Technical Implementationโ
Logic Flowโ
Input.cart.line_items.each do |line_item|
# Only apply if no price match was found
if !price_match_applied && line_item.variant.compare_at_price
compare_price = line_item.variant.compare_at_price.cents
original_price = line_item.variant.price.cents
# Only apply if it's actually a discount
if compare_price < original_price
discount_percent = calculate_percent(original_price, compare_price)
apply_discount(line_item, compare_price, "Sale! #{discount_percent}% off!")
end
end
end
Percentage Calculationโ
def calculate_percent(original_price, new_price)
return 0 if original_price <= 0
discount_amount = original_price - new_price
((discount_amount.to_f / original_price) * 100).round
end
Helper Methodโ
def apply_discount(line_item, price, message)
new_line_price = Money.new(cents: price) * line_item.quantity
line_item.change_line_price(new_line_price, message: message)
end
Setting Compare At Priceโ
Via Shopify Adminโ
- Go to product variant
- Set "Compare at price" field
- Save
Via Frontend Displayโ
The frontend's calculateStrikeOutPrice function handles display logic. See Price Display for details.
Money Object Usageโ
- All price comparisons use Shopify's
Moneyobject - Prices stored internally in cents
- Ensures accurate decimal handling
Guest Handlingโ
โ Guests can receive compare at price discounts since no customer tags are required.
Priorityโ
Compare at price has lower priority than price matching.
If both are available:
- Price match is applied โ
- Compare at price is skipped