Skip to content

Allow strikethrough prices to be calculated for variants#6287

Open
mamhoff wants to merge 10 commits intosolidusio:mainfrom
mamhoff:solidus-promotion-discounted-prices
Open

Allow strikethrough prices to be calculated for variants#6287
mamhoff wants to merge 10 commits intosolidusio:mainfrom
mamhoff:solidus-promotion-discounted-prices

Conversation

@mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Jun 17, 2025

Summary

Currently, the promotions system acts on the customer's Spree::Order record when it changes. Spree::Order#recalculate will compute promotion eligibility and apply any discounts.

This change allows "previewing" what promotions would do to the price of a variant, before putting it into the cart. This allows for the popular "strikethrough" prices one sees on many sites. For example, Amazon:

grafik

The result of these calculcations depend on the current order (with the current user attached as order.user) so that any promotions that are active for the current order apply. The order can be nil.

Prices are display ads - so the class that changes the price display to include discounts is called an "advertiser".

That means we cannot persist the discounts on variant prices, but instead need to calculate them before display. I've opted - for now, this is up for debate - to run a SolidusPromotions::ProductAdvertiser on a product before displaying. Other services classes that would be able to compute strikethrough prices for taxons ("Hoovers - up to 39% off!") would have a similar implementation.

Currently this works as follows:

# PDP controller
def show
   @product = Spree::Product.find_by(params[:slug])

   SolidusPromotions::ProductAdvertiser.new(product: @product, order: current_order, quantity: 1).call
end
<-- products/show.html.erb -->
<% price = product.master.price_for_options(current_pricing_options) %>
<span class="pill strikethrough">
  <%= price.display_amount.to_html %>
</span>
<span class="price">
  <%= price.display_discounted_amount.to_html %>
</span> 

The main implementation idea is that line items, shipments and shipping rates have an amount column that all the calculations in the promotions module apply to, and that we can leverage Spree::Price#amount in the same way for variants.

This kind of previewing can be done with the new promotion benefit SolidusPromotions::Benefit::AdvertisePrice. Only three calculators are supported right now: FlatRate, Percent and FlexiRate. I'm pretty sure these are by far the most popular calculators for promotions and carry 90% of the load.

Because the logic is the same as with line items and shipments, this should take into account the whole complexity of the promotion system - lanes, stackable discounts, and so on.

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@github-actions github-actions bot added changelog:solidus_core Changes to the solidus_core gem changelog:solidus_promotions Changes to the solidus_promotions gem labels Jun 17, 2025
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from d539ec6 to 0a425c2 Compare June 25, 2025 11:18
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from 0a425c2 to 0c5e751 Compare July 2, 2025 18:26
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from 0c5e751 to 373eb72 Compare October 14, 2025 07:40
@codecov
Copy link

codecov bot commented Oct 14, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.59%. Comparing base (997f46a) to head (0fc1423).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6287      +/-   ##
==========================================
+ Coverage   89.51%   89.59%   +0.07%     
==========================================
  Files         981      987       +6     
  Lines       20504    20651     +147     
==========================================
+ Hits        18355    18502     +147     
  Misses       2149     2149              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch 2 times, most recently from e9dec78 to 0a8e967 Compare October 14, 2025 09:20
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from 0a8e967 to 3b1ae8c Compare October 27, 2025 11:59
@adammathys adammathys self-requested a review October 29, 2025 16:35
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from 3b1ae8c to 1a32f17 Compare October 30, 2025 14:17
@mamhoff mamhoff changed the title Solidus promotion discounted prices Allow strikethrough prices to be calculated for variants Oct 30, 2025
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from 1a32f17 to 2dfcd98 Compare March 16, 2026 15:18
@github-actions github-actions bot removed the changelog:solidus_core Changes to the solidus_core gem label Mar 16, 2026
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch 2 times, most recently from 2003381 to 2a8ad25 Compare March 16, 2026 16:16
@mamhoff mamhoff marked this pull request as ready for review March 16, 2026 16:16
@mamhoff mamhoff requested a review from a team as a code owner March 16, 2026 16:16
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from 2a8ad25 to 678c947 Compare March 17, 2026 07:01
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing! I have a few nits, but can't wait to have this supported :)

@tvdeyen tvdeyen added this to the 4.7 milestone Mar 17, 2026
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from 678c947 to a917ada Compare March 17, 2026 15:18
mamhoff added 2 commits March 17, 2026 16:30
Taking advantage of the fact the `Spree::Price` objects have an `amount`
method, we can add an array of `discounts` (in-memory
`SolidusPromotions::ItemDiscount` objects) and thereby calculate a
`discounted_amount` along with the amount. This is the basis for any
strikethrough pricing.
As straightforward as adding a method alias - and a little
metaprogramming for objects that respond to `#currency` directly rather
than through an order.
mamhoff added 8 commits March 17, 2026 16:30
This benefit can discount prices, and has its own
set of conditions. It's implemented separate from `AdjustLineItem`,
because the conditions for displaying the discounted price might
actually differ from what's applied in the cart.
This condition adds the capability of restricting price discounts to
certain products, or disallowing certain products from receiving a price
discount.
This service class will take an order and a product, and discount the
product's variant prices according to currently valid promotions.
Because price_for_options works by reference, discounts applied
will stay on the prices.
This way we can restrict price discounts to specified taxons.
With this we can restrict price discounts by product and option value.
We need to make sure that the FlatRate discount is not applied twice to
the same line item.

For now, I'm only considering the standard case of the same variant
always being on the same line item here. If the store implementing this
has more complex requirements, please adapt the calculator or write a
custom one.
This turned out to be rather complicated.
@mamhoff mamhoff force-pushed the solidus-promotion-discounted-prices branch from a917ada to 0fc1423 Compare March 17, 2026 15:30
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exciting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_promotions Changes to the solidus_promotions gem

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants