Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Add `apply_only_on_full_priced_items` preference to `FlatRate`, `Flex… (#12068)

Damian Legawiecba122e790284
diff --git a/core/app/models/spree/calculator/flat_rate.rb b/core/app/models/spree/calculator/flat_rate.rb
index c1398b0ca9..53125b39ec 100644
--- a/core/app/models/spree/calculator/flat_rate.rb
+++ b/core/app/models/spree/calculator/flat_rate.rb
@@ -4,12 +4,15 @@ module Spree
class Calculator::FlatRate < Calculator
preference :amount, :decimal, default: 0
preference :currency, :string, default: -> { Spree::Store.default.default_currency }
+ preference :apply_only_on_full_priced_items, :boolean, default: false
def self.description
Spree.t(:flat_rate_per_order)
end
def compute(object = nil)
+ return 0 if preferred_apply_only_on_full_priced_items && object&.variant&.compare_at_amount_in(object.currency).present?
+
if object && preferred_currency.casecmp(object.currency.upcase).zero?
preferred_amount
else
diff --git a/core/app/models/spree/calculator/flexi_rate.rb b/core/app/models/spree/calculator/flexi_rate.rb
index 27fae73e6e..4fcd344389 100644
--- a/core/app/models/spree/calculator/flexi_rate.rb
+++ b/core/app/models/spree/calculator/flexi_rate.rb
@@ -6,6 +6,7 @@ module Spree
preference :additional_item, :decimal, default: 0.0
preference :max_items, :integer, default: 0
preference :currency, :string, default: -> { Spree::Store.default.default_currency }
+ preference :apply_only_on_full_priced_items, :boolean, default: false
def self.description
Spree.t(:flexible_rate)
@@ -16,6 +17,8 @@ module Spree
end
def compute(object)
+ return 0 if preferred_apply_only_on_full_priced_items && object.variant.compare_at_amount_in(object.currency).present?
+
compute_from_quantity(object.quantity)
end
diff --git a/core/app/models/spree/calculator/percent_on_line_item.rb b/core/app/models/spree/calculator/percent_on_line_item.rb
index ce99a2e1b3..d400dffbb9 100644
--- a/core/app/models/spree/calculator/percent_on_line_item.rb
+++ b/core/app/models/spree/calculator/percent_on_line_item.rb
@@ -2,12 +2,15 @@ module Spree
class Calculator
class PercentOnLineItem < Calculator
preference :percent, :decimal, default: 0
+ preference :apply_only_on_full_priced_items, :boolean, default: false
def self.description
Spree.t(:percent_per_item)
end
def compute(object)
+ return 0 if preferred_apply_only_on_full_priced_items && object.variant.compare_at_amount_in(object.currency).present?
+
computed_amount = (object.amount * preferred_percent / 100).round(2)
# We don't want to cause the promotion adjustments to push the order into a negative total.
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 5c123afe7f..133cc3dac4 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -576,6 +576,7 @@ en:
alternative_phone: Alternative Phone
amount: Amount
and: and
+ apply_only_on_full_priced_items: Apply only on full priced items
approve: approve
approver: Approver
approved_at: Approved at
diff --git a/core/spec/models/spree/calculator/flat_rate_spec.rb b/core/spec/models/spree/calculator/flat_rate_spec.rb
index 638f62286c..4ed51ea271 100644
--- a/core/spec/models/spree/calculator/flat_rate_spec.rb
+++ b/core/spec/models/spree/calculator/flat_rate_spec.rb
@@ -2,43 +2,65 @@ require 'spec_helper'
describe Spree::Calculator::FlatRate, type: :model do
let(:calculator) { Spree::Calculator::FlatRate.new }
- let(:order) { create(:order) }
+ let(:line_item) { create(:line_item) }
- before { allow(order).to receive_messages quantity: 10 }
+ before { allow(line_item).to receive_messages quantity: 10 }
- context 'compute' do
- it "computes the amount as the rate when currency matches the order's currency" do
- calculator.preferred_amount = 25.0
- calculator.preferred_currency = 'GBP'
- allow(order).to receive_messages currency: 'GBP'
- expect(calculator.compute(order).round(2)).to eq(25.0)
- end
+ describe '#compute' do
+ it { expect(calculator.preferred_apply_only_on_full_priced_items).to be false }
- it "computes the amount as 0 when currency does not match the order's currency" do
- calculator.preferred_amount = 100.0
- calculator.preferred_currency = 'GBP'
- allow(order).to receive_messages currency: 'USD'
- expect(calculator.compute(order).round(2)).to eq(0.0)
- end
+ shared_examples 'computing amount correctly' do
+ it "computes the amount as the rate when currency matches the line_item's currency" do
+ calculator.preferred_amount = 25.0
+ calculator.preferred_currency = 'GBP'
+ allow(line_item).to receive_messages currency: 'GBP'
+ expect(calculator.compute(line_item).round(2)).to eq(25.0)
+ end
- it 'computes the amount as 0 when currency is blank' do
- calculator.preferred_amount = 100.0
- calculator.preferred_currency = ''
- allow(order).to receive_messages currency: 'GBP'
- expect(calculator.compute(order).round(2)).to eq(0.0)
- end
+ it "computes the amount as 0 when currency does not match the line_item's currency" do
+ calculator.preferred_amount = 100.0
+ calculator.preferred_currency = 'GBP'
+ allow(line_item).to receive_messages currency: 'USD'
+ expect(calculator.compute(line_item).round(2)).to eq(0.0)
+ end
+
+ it 'computes the amount as 0 when currency is blank' do
+ calculator.preferred_amount = 100.0
+ calculator.preferred_currency = ''
+ allow(line_item).to receive_messages currency: 'GBP'
+ expect(calculator.compute(line_item).round(2)).to eq(0.0)
+ end
+
+ it 'computes the amount as the rate when the currencies use different casing' do
+ calculator.preferred_amount = 100.0
+ calculator.preferred_currency = 'gBp'
+ allow(line_item).to receive_messages currency: 'GBP'
+ expect(calculator.compute(line_item).round(2)).to eq(100.0)
+ end
- it 'computes the amount as the rate when the currencies use different casing' do
- calculator.preferred_amount = 100.0
- calculator.preferred_currency = 'gBp'
- allow(order).to receive_messages currency: 'GBP'
- expect(calculator.compute(order).round(2)).to eq(100.0)
+ it 'computes the amount as 0 when there is no object' do
+ calculator.preferred_amount = 100.0
+ calculator.preferred_currency = 'GBP'
+ expect(calculator.compute.round(2)).to eq(0.0)
+ end
end
- it 'computes the amount as 0 when there is no object' do
- calculator.preferred_amount = 100.0
- calculator.preferred_currency = 'GBP'
- expect(calculator.compute.round(2)).to eq(0.0)
+ it_behaves_like 'computing amount correctly'
+
+ context 'when apply_only_on_full_priced_items is true' do
+ before { calculator.preferred_apply_only_on_full_priced_items = true }
+
+ context 'when line item has compare at price' do
+ before { allow(line_item).to receive_messages variant: double(compare_at_amount_in: 10) }
+
+ it 'returns 0' do
+ expect(calculator.compute(line_item)).to eq(0.0)
+ end
+ end
+
+ context 'when line item does not have compare at price' do
+ it_behaves_like 'computing amount correctly'
+ end
end
end
end
diff --git a/core/spec/models/spree/calculator/flexi_rate_spec.rb b/core/spec/models/spree/calculator/flexi_rate_spec.rb
index 4819165054..5f1d5116e6 100644
--- a/core/spec/models/spree/calculator/flexi_rate_spec.rb
+++ b/core/spec/models/spree/calculator/flexi_rate_spec.rb
@@ -2,37 +2,52 @@ require 'spec_helper'
describe Spree::Calculator::FlexiRate, type: :model do
let(:calculator) { Spree::Calculator::FlexiRate.new }
- let(:order) { create(:order) }
+ let(:line_item) { create(:line_item) }
- before { allow(order).to receive_messages quantity: 10 }
+ before { allow(line_item).to receive_messages quantity: 10 }
- context 'compute' do
- it 'computes amount correctly when all fees are 0' do
- expect(calculator.compute(order).round(2)).to eq(0.0)
- end
+ describe '#compute' do
+ it { expect(calculator.preferred_apply_only_on_full_priced_items).to be false }
- it 'computes amount correctly when first_item has a value' do
- allow(calculator).to receive_messages preferred_first_item: 1.0
- expect(calculator.compute(order).round(2)).to eq(1.0)
- end
+ shared_examples 'computes amount correctly' do
+ it 'computes amount correctly when all fees are 0' do
+ expect(calculator.compute(line_item).round(2)).to eq(0.0)
+ end
- it 'computes amount correctly when additional_items has a value' do
- allow(calculator).to receive_messages preferred_additional_item: 1.0
- expect(calculator.compute(order).round(2)).to eq(9.0)
- end
+ it 'computes amount correctly when first_item has a value' do
+ allow(calculator).to receive_messages preferred_first_item: 1.0
+ expect(calculator.compute(line_item).round(2)).to eq(1.0)
+ end
- it 'computes amount correctly when additional_items and first_item have values' do
- allow(calculator).to receive_messages preferred_first_item: 5.0, preferred_additional_item: 1.0
- expect(calculator.compute(order).round(2)).to eq(14.0)
- end
+ it 'computes amount correctly when additional_items has a value' do
+ allow(calculator).to receive_messages preferred_additional_item: 1.0
+ expect(calculator.compute(line_item).round(2)).to eq(9.0)
+ end
- it 'computes amount correctly when additional_items and first_item have values AND max items has value' do
- allow(calculator).to receive_messages preferred_first_item: 5.0, preferred_additional_item: 1.0, preferred_max_items: 3
- expect(calculator.compute(order).round(2)).to eq(7.0)
+ it 'computes amount correctly when additional_items and first_item have values' do
+ allow(calculator).to receive_messages preferred_first_item: 5.0, preferred_additional_item: 1.0
+ expect(calculator.compute(line_item).round(2)).to eq(14.0)
+ end
+
+ it 'computes amount correctly when additional_items and first_item have values AND max items has value' do
+ allow(calculator).to receive_messages preferred_first_item: 5.0, preferred_additional_item: 1.0, preferred_max_items: 3
+ expect(calculator.compute(line_item).round(2)).to eq(7.0)
+ end
+
+ it 'allows creation of new object with all the attributes' do
+ Spree::Calculator::FlexiRate.new(preferred_first_item: 1, preferred_additional_item: 1, preferred_max_items: 1)
+ end
end
- it 'allows creation of new object with all the attributes' do
- Spree::Calculator::FlexiRate.new(preferred_first_item: 1, preferred_additional_item: 1, preferred_max_items: 1)
+ context 'when apply_only_on_full_priced_items is true' do
+ before { allow(calculator).to receive_messages preferred_apply_only_on_full_priced_items: true }
+
+ it 'returns 0' do
+ allow(line_item).to receive_messages variant: double('Variant', compare_at_amount_in: 1.0)
+ expect(calculator.compute(line_item)).to eq(0.0)
+ end
+
+ it_behaves_like 'computes amount correctly'
end
end
end
diff --git a/core/spec/models/spree/calculator/percent_on_line_item_spec.rb b/core/spec/models/spree/calculator/percent_on_line_item_spec.rb
index 19c8ce8338..e988a16f65 100644
--- a/core/spec/models/spree/calculator/percent_on_line_item_spec.rb
+++ b/core/spec/models/spree/calculator/percent_on_line_item_spec.rb
@@ -6,20 +6,44 @@ describe Spree::Calculator::PercentOnLineItem, type: :model do
before { allow(calculator).to receive_messages preferred_percent: 10 }
- context 'compute' do
- it 'rounds result correctly' do
- allow(line_item).to receive_messages amount: 31.08
- expect(calculator.compute(line_item)).to eq 3.11
+ describe '#compute' do
+ it { expect(calculator.preferred_apply_only_on_full_priced_items).to be false }
- allow(line_item).to receive_messages amount: 31.00
- expect(calculator.compute(line_item)).to eq 3.10
+ shared_examples 'computing amount correctly' do
+ it 'rounds result correctly' do
+ allow(line_item).to receive_messages amount: 31.08
+ expect(calculator.compute(line_item)).to eq 3.11
+
+ allow(line_item).to receive_messages amount: 31.00
+ expect(calculator.compute(line_item)).to eq 3.10
+ end
+
+ it 'returns object.amount if computed amount is greater' do
+ allow(line_item).to receive_messages amount: 30.00
+ allow(calculator).to receive_messages preferred_percent: 110
+
+ expect(calculator.compute(line_item)).to eq 30.0
+ end
end
- it 'returns object.amount if computed amount is greater' do
- allow(line_item).to receive_messages amount: 30.00
- allow(calculator).to receive_messages preferred_percent: 110
+ it_behaves_like 'computing amount correctly'
+
+ context 'when apply_only_on_full_priced_items preference is true' do
+ before { allow(calculator).to receive_messages preferred_apply_only_on_full_priced_items: true }
+
+ context 'when line item has a compare at price' do
+ before { allow(line_item).to receive_messages variant: double(compare_at_amount_in: 10) }
+
+ it 'returns 0' do
+ expect(calculator.compute(line_item)).to eq 0
+ end
+ end
+
+ context 'when line item does not have a compare at price' do
+ before { allow(line_item).to receive_messages amount: 31.00 }
- expect(calculator.compute(line_item)).to eq 30.0
+ it_behaves_like 'computing amount correctly'
+ end
end
end
end