Skip to main content

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_price set 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โ€‹

FeatureCompare At PricePrice Matching
EligibilityEveryoneTagged customers only
Message"Sale! X% off!"Campaign-specific message
Personalization SurchargeโŒ Noโœ… Yes (+$6)
PriorityLowerHigher
SetupSet in Shopify variantCustomer + 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โ€‹

  1. Go to product variant
  2. Set "Compare at price" field
  3. 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 Money object
  • 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:

  1. Price match is applied โœ…
  2. Compare at price is skipped