Skip to content

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.

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::ProductDiscounter 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::ProductDiscounter.new(product: @product, order: current_order, quantity: 1).call
end
<-- products/show.html.erb -->
<span class="pill strikethrough">
  <%= product.undiscounted_price.to_html %>
</span>
<span class="price">
  <%= product.discounted_price.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::AdjustPrice. 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

❌ Patch coverage is 99.34211% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.40%. Comparing base (f418798) to head (1a32f17).

Files with missing lines Patch % Lines
...pp/models/solidus_promotions/product_discounter.rb 96.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6287      +/-   ##
==========================================
+ Coverage   89.34%   89.40%   +0.06%     
==========================================
  Files         961      967       +6     
  Lines       20165    20292     +127     
==========================================
+ Hits        18016    18142     +126     
- Misses       2149     2150       +1     

☔ 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
This way we get good formatting, currency support, all the nice things.
Sometimes, we need to calculate the adjustments given before a
particular promotion runs: When calculating a price, we need a line
item's discountable amount but only up to the previous lane. In order to
get that, we need to know which lanes come before the current promotion.
That's what this commit gives us.
This is to aid us in finding the amount the line item is adjusted by at
the current lane.
For the upcoming `AdjustPrices` Benefit, we need to have a set of
conditions that can be used with both prices and line items. It's
currently set to be empty, but will be filled as we expand the list of
conditions that can be used for both line items and prices.

Both need to be supported so that promotions don't have surprising
results.
This benefit can adjust prices as well as line items, and has its own
set of conditions.
This has the same API - just adding tests and refactoring for
legibility here.
Also adds specs, which were not there before.
In order to calculate a flat rate discount for a price, we need to do a
few different things from computing a flat rate discount for a line item
or shipment.
@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
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.
Prices have their currency on them, we don't need the order. Price
calculation will, however, pass in some options to `compute`, so let's
account for that.
This turned out to be rather complicated.
This service class will take an order and a product, and discount it
according to currently valid promotions and according to the pricing
options for the current context.
@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_core Changes to the solidus_core gem changelog:solidus_promotions Changes to the solidus_promotions gem

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant