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.

Fix `Spree::ShippingRate#free?` (#13267)

Łukasz Adamczyk2fae632cf873
diff --git a/core/app/models/spree/shipping_rate.rb b/core/app/models/spree/shipping_rate.rb
index 62fd3242cf..944bd5dd50 100644
--- a/core/app/models/spree/shipping_rate.rb
+++ b/core/app/models/spree/shipping_rate.rb
@@ -7,9 +7,9 @@ module Spree
money_methods :base_price, :final_price, :tax_amount
- delegate :order, :currency, :free?, :with_free_shipping_promotion?, to: :shipment
- delegate :name, to: :shipping_method
- delegate :code, to: :shipping_method, prefix: true
+ delegate :order, :currency, :with_free_shipping_promotion?, to: :shipment
+ delegate :name, to: :shipping_method
+ delegate :code, to: :shipping_method, prefix: true
def display_price
price = display_base_price.to_s
@@ -27,6 +27,10 @@ module Spree
alias display_cost display_price
alias_attribute :base_price, :cost
+ def free?
+ final_price.zero?
+ end
+
def tax_amount
@tax_amount ||= tax_rate&.calculator&.compute_shipping_rate(self) || BigDecimal(0)
end
diff --git a/core/lib/spree/testing_support/factories/calculator_factory.rb b/core/lib/spree/testing_support/factories/calculator_factory.rb
index aac6bbe037..061b34518d 100644
--- a/core/lib/spree/testing_support/factories/calculator_factory.rb
+++ b/core/lib/spree/testing_support/factories/calculator_factory.rb
@@ -11,11 +11,11 @@ FactoryBot.define do
end
factory :shipping_calculator, class: Spree::Calculator::Shipping::FlatRate do
- after(:create) { |c| c.set_preference(:amount, 10.0) }
+ preferred_amount { 10.0 }
end
factory :shipping_no_amount_calculator, class: Spree::Calculator::Shipping::FlatRate do
- after(:create) { |c| c.set_preference(:amount, 0) }
+ preferred_amount { 0 }
end
factory :flat_rate_calculator, class: Spree::Calculator::FlatRate do
diff --git a/core/spec/lib/tasks/exchanges_spec.rb b/core/spec/lib/tasks/exchanges_spec.rb
index 3f5e26dade..b8fce039d1 100644
--- a/core/spec/lib/tasks/exchanges_spec.rb
+++ b/core/spec/lib/tasks/exchanges_spec.rb
@@ -78,7 +78,7 @@ describe 'exchanges:charge_unreturned_items' do
it 'authorizes the order for the full amount of the unreturned items including taxes' do
expect { subject.invoke }.to change { Spree::Payment.count }.by(1)
new_order = Spree::Order.last
- expected_amount = return_item_2.reload.exchange_variant.price + new_order.additional_tax_total + new_order.included_tax_total
+ expected_amount = return_item_2.reload.exchange_variant.price + new_order.additional_tax_total + new_order.included_tax_total + new_order.ship_total
expect(new_order.total).to eq expected_amount
payment = new_order.payments.first
expect(payment.amount).to eq expected_amount
diff --git a/core/spec/models/spree/order/payment_spec.rb b/core/spec/models/spree/order/payment_spec.rb
index 221bf37491..b4f16003d7 100644
--- a/core/spec/models/spree/order/payment_spec.rb
+++ b/core/spec/models/spree/order/payment_spec.rb
@@ -227,19 +227,20 @@ module Spree
end
it 'incorporates refund reimbursements' do
- # Creates an order w/total 10
+ # Creates an order w/total 20
reimbursement = create :reimbursement
- # Set the payment amount to actually be the order total of 10
- reimbursement.order.payments.first.update_column :amount, 10
- # Creates a refund of 10
- create :refund, amount: 10,
- payment: reimbursement.order.payments.first,
+ order = reimbursement.order
+ # Set the payment amount to actually be the order total of 20
+ order.payments.first.update_column :amount, order.total
+ # Creates a refund of 20
+ create :refund, amount: order.total,
+ payment: order.payments.first,
reimbursement: reimbursement
order = reimbursement.order.reload
# Update the order totals so payment_total goes to 0 reflecting the refund..
order.update_with_updater!
# Order Total - (Payment Total + Reimbursed)
- # 10 - (0 + 10) = 0
+ # 20 - (0 + 20) = 0
expect(order.outstanding_balance).to eq 0
end
diff --git a/core/spec/models/spree/shipping_method_spec.rb b/core/spec/models/spree/shipping_method_spec.rb
index c63d239fcc..c3a7d4e2cc 100644
--- a/core/spec/models/spree/shipping_method_spec.rb
+++ b/core/spec/models/spree/shipping_method_spec.rb
@@ -211,12 +211,12 @@ describe Spree::ShippingMethod, type: :model do
end
describe '#display_estimated_price' do
- it { expect(shipping_method.display_estimated_price).to eq('Flat rate: Free') }
+ it { expect(shipping_method.display_estimated_price).to eq('Flat rate: $10.00') }
- context 'with calculator' do
- let(:shipping_method) { build(:shipping_method, calculator: create(:shipping_calculator)) }
+ context 'with the free rate' do
+ let(:shipping_method) { build(:free_shipping_method) }
- it { expect(shipping_method.display_estimated_price).to eq('Flat rate: $10.00') }
+ it { expect(shipping_method.display_estimated_price).to eq('Flat rate: Free') }
end
end
end
diff --git a/core/spec/models/spree/shipping_rate_spec.rb b/core/spec/models/spree/shipping_rate_spec.rb
index 7abbf7a69c..6fdce5c34b 100644
--- a/core/spec/models/spree/shipping_rate_spec.rb
+++ b/core/spec/models/spree/shipping_rate_spec.rb
@@ -198,4 +198,42 @@ describe Spree::ShippingRate, type: :model do
expect(shipping_rate.display_delivery_range).to eq('Delivery in 1-2 business days')
end
end
+
+ describe '#free?' do
+ subject { shipping_rate.free? }
+
+ context 'when the shipping rate cost is 0' do
+ let(:shipping_rate) { create(:shipping_rate, cost: 0) }
+
+ it { is_expected.to be(true) }
+ end
+
+ context 'when the shipping rate cost is not 0' do
+ let(:shipping_rate) { create(:shipping_rate, cost: 10) }
+
+ let(:shipment) { shipping_rate.shipment }
+ let(:order) { shipment.order }
+
+ it { is_expected.to be(false) }
+
+ context 'when the shipment has a free shipping promotion' do
+ let(:free_shipping_promotion) { create(:free_shipping_promotion) }
+
+ before do
+ order.coupon_code = free_shipping_promotion.code
+ Spree::PromotionHandler::Coupon.new(order).apply
+ end
+
+ it { is_expected.to be(true) }
+ end
+
+ context 'when the discount amount is equal to the cost' do
+ before do
+ allow(shipping_rate).to receive(:discount_amount).and_return(-10.0)
+ end
+
+ it { is_expected.to be(true) }
+ end
+ end
+ end
end
diff --git a/core/spec/services/spree/checkout/advance_spec.rb b/core/spec/services/spree/checkout/advance_spec.rb
index 7ef1614a78..740347c16f 100644
--- a/core/spec/services/spree/checkout/advance_spec.rb
+++ b/core/spec/services/spree/checkout/advance_spec.rb
@@ -8,8 +8,8 @@ RSpec.describe Spree::Checkout::Advance do
describe '#call' do
context 'with no specific target state' do
before do
- order.update!(total: 19.99, payment_total: 0, shipment_total: 5, item_total: 14.99, completed_at: nil)
- order.payments.last.update!(amount: 19.99)
+ order.update!(total: 29.99, payment_total: 0, shipment_total: 10.00, item_total: 19.99, completed_at: nil)
+ order.payments.first.update!(amount: 29.99)
order.reload
Spree::StockItem.where(variant: order.variants).update_all(count_on_hand: 10, backorderable: false)
diff --git a/storefront/spec/features/checkout_steps_spec.rb b/storefront/spec/features/checkout_steps_spec.rb
index 03d4de773d..1a558970f1 100644
--- a/storefront/spec/features/checkout_steps_spec.rb
+++ b/storefront/spec/features/checkout_steps_spec.rb
@@ -306,7 +306,7 @@ describe 'Checkout steps (address and delivery)' do
let!(:promotion) { create(:free_shipping_promotion, code: 'freeshipping') }
let!(:order) { create(:order_with_line_items, line_items_count: 1, user: user, ship_address: user.ship_address, bill_address: user.ship_address) }
let!(:shipping_method) do
- create(:shipping_method, name: 'Shipping Method', code: 'shipping_method', calculator: create(:shipping_calculator))
+ create(:shipping_method, name: 'Shipping Method', code: 'shipping_method', calculator: create(:shipping_calculator, preferred_amount: 15))
end
let!(:other_shipping_method) do
create(
@@ -336,11 +336,11 @@ describe 'Checkout steps (address and delivery)' do
choose "order_ship_address_id_#{user.shipping_address.id}"
click_on 'Save and Continue'
- expect(find('#summary-order-total')).to have_content('$19.99')
+ expect(find('#summary-order-total')).to have_content('$29.99')
- page.find("input[data-cost='$10.00']").click
+ page.find("input[data-cost='$15.00']").click
wait_for_turbo
- expect(find('#summary-order-total')).to have_content('$29.99')
+ expect(find('#summary-order-total')).to have_content('$34.99')
page.find("input[data-cost='$20.00']").click
wait_for_turbo
@@ -358,8 +358,8 @@ describe 'Checkout steps (address and delivery)' do
choose "order_ship_address_id_#{user.shipping_address.id}"
click_on 'Save and Continue'
- expect(page).to have_selector("input[data-cost='$0.00'][checked='checked']")
- expect(page).to have_selector("input[data-cost='$10.00']")
+ expect(page).to have_selector("input[data-cost='$10.00'][checked='checked']")
+ expect(page).to have_selector("input[data-cost='$15.00']")
fill_in 'coupon_code', with: promotion.code
click_on 'Apply'
@@ -370,7 +370,7 @@ describe 'Checkout steps (address and delivery)' do
expect(page).to have_content("#{Spree.t(:total)}\nUSD $19.99")
end
- page.find("input[data-cost='$10.00']").click
+ page.find("input[data-cost='$15.00']").click
within('div.summary-content') do
expect(page).to have_content('Free')
@@ -398,7 +398,7 @@ describe 'Checkout steps (address and delivery)' do
expect(page).to have_content('Delivery')
expect(page).to have_content(Spree::ShippingMethod.first.name)
- page.find("input[data-cost='$10.00']").click
+ page.find("input[data-cost='$15.00']").click
expect(page).to have_content('Payment')
end
@@ -409,7 +409,7 @@ describe 'Checkout steps (address and delivery)' do
let(:user) { create(:user_with_addresses) }
let!(:order) { create(:order_with_line_items, line_items_count: 1, user: user, ship_address: user.ship_address, bill_address: user.ship_address) }
let!(:shipping_method) do
- create(:shipping_method, name: 'Shipping Method', code: 'shipping_method', calculator: create(:shipping_calculator))
+ create(:shipping_method, name: 'Shipping Method', code: 'shipping_method', calculator: create(:shipping_calculator, preferred_amount: 15))
end
before do
@@ -420,13 +420,13 @@ describe 'Checkout steps (address and delivery)' do
visit "/checkout/#{order.token}"
choose "order_ship_address_id_#{user.shipping_address.id}"
click_on 'Save and Continue'
- page.find("input[data-cost='$10.00']").click
+ page.find("input[data-cost='$15.00']").click
click_on 'Save and Continue'
- expect(page).to have_content('Shipping Method · $10.00')
+ expect(page).to have_content('Shipping Method · $15.00')
expect(page).to have_content("Subtotal:\n$19.99")
- expect(page).to have_content("Shipping:\n$10.00")
- expect(page).to have_content("Total\nUSD $29.99")
+ expect(page).to have_content("Shipping:\n$15.00")
+ expect(page).to have_content("Total\nUSD $34.99")
uncheck 'Use Shipping Address'
fill_in_address_form('order_bill_address_attributes_country_id')
@@ -445,15 +445,15 @@ describe 'Checkout steps (address and delivery)' do
visit "/checkout/#{order.token}"
choose "order_ship_address_id_#{user.shipping_address.id}"
click_on 'Save and Continue'
- page.find("input[data-cost='$10.00']").click
+ page.find("input[data-cost='$15.00']").click
click_on 'Save and Continue'
- expect(page).to have_content('Shipping Method · $10.00')
- expect(page).to have_content("Show\norder summary\n$29.99")
+ expect(page).to have_content('Shipping Method · $15.00')
+ expect(page).to have_content("Show\norder summary\n$34.99")
click_on 'toggle-order-summary'
expect(page).to have_content("Subtotal:\n$19.99")
- expect(page).to have_content("Shipping:\n$10.00")
- expect(page).to have_content("Total\nUSD $29.99")
+ expect(page).to have_content("Shipping:\n$15.00")
+ expect(page).to have_content("Total\nUSD $34.99")
uncheck 'Use Shipping Address'
fill_in_address_form('order_bill_address_attributes_country_id')