Allow strikethrough prices to be calculated for variants
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:
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:
- [x] I agree that my PR will be published under the same license as Solidus.
- [x] I have written a thorough PR description.
- [x] I have kept my commits small and atomic.
- [x] I have used clear, explanatory commit messages.
The following are not always needed:
Codecov Report
:x: Patch coverage is 99.34211% with 1 line in your changes missing coverage. Please review.
:white_check_mark: 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 :warning: |
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
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.