Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix calculating discounts for Spree::Cart as well as for Spree::Order (#14587)
Kacper Mękarskiba32a053b002
diff --git a/spree/common_spree_dependencies.rb b/spree/common_spree_dependencies.rbindex 9d5a8d44db..10469f7afb 100644--- a/spree/common_spree_dependencies.rb+++ b/spree/common_spree_dependencies.rb@@ -6,6 +6,12 @@ source 'https://rubygems.org'gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]gem 'rails', ENV.fetch('RAILS_VERSION', '~> 8.1.0'), require: false+# Held below 3.0: json 3.0 made JSON.parse's options keyword-only, and Active+# Support 8.1 still passes them positionally, so reading any JSON column raises+# ArgumentError — which is every metadata column we have. Drop the pin once+# Rails ships a release that calls the new signature.+gem 'json', '< 3'+platforms :jruby dogem 'jruby-openssl'enddiff --git a/spree/core/app/models/concerns/spree/purchase/totals.rb b/spree/core/app/models/concerns/spree/purchase/totals.rbindex 7049bc4905..991252b9a3 100644--- a/spree/core/app/models/concerns/spree/purchase/totals.rb+++ b/spree/core/app/models/concerns/spree/purchase/totals.rb@@ -10,6 +10,11 @@ module Spreeline_items.sum(:quantity)end+ # @return [BigDecimal]+ def amount+ line_items.sum(BigDecimal('0'), &:amount)+ end+# Re-sums what the customer has actually paid, and nothing else. A# payment settling moves only the payment side of the ledger — item# and delivery money is the totals workflow's business, anddiff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rbindex 300cb5dee4..9e2c96492f 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -358,11 +358,6 @@ module Spreeupdate_hooks.add(hook)end- # For compatibility with Calculator::PriceSack- def amount- line_items.inject(0.0) { |sum, li| sum + li.amount }- end-# Sum of the eligible promotion adjustments applied to the order itself# (whole-order discounts created by Promotion::Actions::CreateAdjustment,# distributed proportionally across line items), as opposed to promotionsdiff --git a/spree/core/spec/models/concerns/spree/purchase/totals_spec.rb b/spree/core/spec/models/concerns/spree/purchase/totals_spec.rbindex 2df4aee401..6ff390d175 100644--- a/spree/core/spec/models/concerns/spree/purchase/totals_spec.rb+++ b/spree/core/spec/models/concerns/spree/purchase/totals_spec.rb@@ -10,6 +10,21 @@ RSpec.shared_examples 'a purchase totals host' doendend+ describe '#amount' do+ it 'sums line item amounts' do+ record = new_record_with_line_items(line_items_count: 2, line_items_price: 2.5)++ expect(record.amount).to eq(5)+ end++ it 'stays a zero BigDecimal with no line items' do+ amount = new_record.amount++ expect(amount).to eq(0)+ expect(amount).to be_a(BigDecimal)+ end+ end+describe '#outstanding_balance?' doit 'reflects a non-zero balance' doexpect(new_record(total: 10.10, payment_total: 9.50).outstanding_balance?).to be(true)@@ -57,8 +72,8 @@ RSpec.describe Spree::Purchase::Totals dobuild(:cart, store: @default_store, **attributes)end- def new_record_with_line_items- create(:cart_with_line_items, store: @default_store)+ def new_record_with_line_items(**attributes)+ create(:cart_with_line_items, store: @default_store, **attributes)enddef create_fulfillment(record)@@ -77,8 +92,8 @@ RSpec.describe Spree::Purchase::Totals dobuild(:order, store: @default_store, **attributes)end- def new_record_with_line_items- create(:order_with_line_items, store: @default_store)+ def new_record_with_line_items(**attributes)+ create(:order_with_line_items, store: @default_store, **attributes)enddef create_fulfillment(record)diff --git a/spree/core/spec/models/spree/order_spec.rb b/spree/core/spec/models/spree/order_spec.rbindex 4b3cc2291b..a25e623301 100644--- a/spree/core/spec/models/spree/order_spec.rb+++ b/spree/core/spec/models/spree/order_spec.rb@@ -967,18 +967,6 @@ describe Spree::Order, type: :model doendend- describe '#amount' do- before do- @order = create(:order, customer: user)- @order.line_items = [create(:line_item, price: 1.0, quantity: 2),- create(:line_item, price: 1.0, quantity: 1)]- end-- it 'returns the correct sum of items' do- expect(@order.amount).to eq(3.0)- end- end-describe '#can_cancel?' doit 'is false for a canceled order' doorder.status = 'canceled'diff --git a/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb b/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rbindex 09440d5ea7..0c0b4b973b 100644--- a/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb+++ b/spree/core/spec/models/spree/promotion/actions/create_adjustment_spec.rb@@ -20,6 +20,23 @@ describe Spree::Promotion::Actions::CreateAdjustment, type: :model dozero = described_class.create!(promotion: promotion, calculator: Spree::Calculator::FlatRate.new(preferred_amount: 0))expect(zero.perform(order: order, promotion: promotion)).to be(false)end++ context 'when the promotable is a cart' do+ let(:cart) { create(:cart_with_line_items, line_items_count: 2, line_items_price: 10, store: promotion.store) }++ {+ 'a flat percent of the item total' => -> { Spree::Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) },+ 'a tiered percent' => -> { Spree::Calculator::TieredPercent.new(preferred_base_percent: 10, preferred_tiers: { 100 => 50 }) },+ 'a tiered flat rate' => -> { Spree::Calculator::TieredFlatRate.new(preferred_base_amount: 2, preferred_currency: 'USD', preferred_tiers: { 100 => 50 }) }+ }.each do |calculator_description, build_calculator|+ it "discounts it by #{calculator_description}" do+ action = described_class.create!(promotion: promotion, calculator: build_calculator.call)++ expect(action.perform(order: cart, promotion: promotion)).to be(true)+ expect(cart.discounts.reload.sum(&:amount)).to eq(-2)+ end+ end+ endenddescribe '#compute_amount' do