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
Add `Spree::LineItem#discounted_price` to calculate the discounted price for the line item (#13256)
Łukasz Adamczyk928a4c363653
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rbindex 4100b15820..c12829a919 100644--- a/core/app/models/spree/line_item.rb+++ b/core/app/models/spree/line_item.rb@@ -82,13 +82,19 @@ module Spreeendextend DisplayMoney- money_methods :amount, :subtotal, :discounted_amount, :final_amount, :total, :price,+ money_methods :amount, :subtotal, :discounted_amount, :final_amount, :total, :price, :discounted_price,:adjustment_total, :additional_tax_total, :promo_total, :included_tax_total,:pre_tax_amount, :shipping_cost, :tax_total, :compare_at_amountalias single_money display_pricealias single_display_amount display_price+ def discounted_price+ return price if quantity.zero?++ price - (promo_total.abs / quantity)+ end+def amountprice * quantityenddiff --git a/core/spec/models/spree/line_item_spec.rb b/core/spec/models/spree/line_item_spec.rbindex 1a995acaa6..149fcfbbaa 100644--- a/core/spec/models/spree/line_item_spec.rb+++ b/core/spec/models/spree/line_item_spec.rb@@ -563,4 +563,36 @@ describe Spree::LineItem, type: :model doexpect(line_item.weight_unit).to eq('kg')endend++ describe '#discounted_price' do+ subject(:discounted_price) { line_item.discounted_price }++ let(:line_item) { create(:line_item, price: 10, quantity: quantity) }+ let(:quantity) { 4 }+ let(:promo_total) { -5 }++ before do+ line_item.update_column(:promo_total, promo_total)+ end++ it 'returns the discounted price for the line item' do+ expect(discounted_price).to eq(8.75)+ end++ context 'when line item promo_total is zero' do+ let(:promo_total) { 0 }++ it 'returns the price for the line item' do+ expect(discounted_price).to eq(10)+ end+ end++ context 'when quantity is zero' do+ let(:quantity) { 0 }++ it 'returns the price for the line item' do+ expect(discounted_price).to eq(10)+ end+ end+ endend