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 `Spree::ShippingRate#free?` (#13267)
Łukasz Adamczyk2fae632cf873
diff --git a/core/app/models/spree/shipping_rate.rb b/core/app/models/spree/shipping_rate.rbindex 62fd3242cf..944bd5dd50 100644--- a/core/app/models/spree/shipping_rate.rb+++ b/core/app/models/spree/shipping_rate.rb@@ -7,9 +7,9 @@ module Spreemoney_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: truedef display_priceprice = display_base_price.to_s@@ -27,6 +27,10 @@ module Spreealias display_cost display_pricealias_attribute :base_price, :cost+ def free?+ final_price.zero?+ end+def tax_amount@tax_amount ||= tax_rate&.calculator&.compute_shipping_rate(self) || BigDecimal(0)enddiff --git a/core/lib/spree/testing_support/factories/calculator_factory.rb b/core/lib/spree/testing_support/factories/calculator_factory.rbindex 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 doendfactory :shipping_calculator, class: Spree::Calculator::Shipping::FlatRate do- after(:create) { |c| c.set_preference(:amount, 10.0) }+ preferred_amount { 10.0 }endfactory :shipping_no_amount_calculator, class: Spree::Calculator::Shipping::FlatRate do- after(:create) { |c| c.set_preference(:amount, 0) }+ preferred_amount { 0 }endfactory :flat_rate_calculator, class: Spree::Calculator::FlatRate dodiff --git a/core/spec/lib/tasks/exchanges_spec.rb b/core/spec/lib/tasks/exchanges_spec.rbindex 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' doit 'authorizes the order for the full amount of the unreturned items including taxes' doexpect { 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_totalexpect(new_order.total).to eq expected_amountpayment = new_order.payments.firstexpect(payment.amount).to eq expected_amountdiff --git a/core/spec/models/spree/order/payment_spec.rb b/core/spec/models/spree/order/payment_spec.rbindex 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 Spreeendit 'incorporates refund reimbursements' do- # Creates an order w/total 10+ # Creates an order w/total 20reimbursement = 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: reimbursementorder = 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) = 0expect(order.outstanding_balance).to eq 0enddiff --git a/core/spec/models/spree/shipping_method_spec.rb b/core/spec/models/spree/shipping_method_spec.rbindex 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 doenddescribe '#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') }endendenddiff --git a/core/spec/models/spree/shipping_rate_spec.rb b/core/spec/models/spree/shipping_rate_spec.rbindex 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 doexpect(shipping_rate.display_delivery_range).to eq('Delivery in 1-2 business days')endend++ 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+ endenddiff --git a/core/spec/services/spree/checkout/advance_spec.rb b/core/spec/services/spree/checkout/advance_spec.rbindex 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 dodescribe '#call' docontext 'with no specific target state' dobefore 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.reloadSpree::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.rbindex 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)' dolet!(: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))endlet!(:other_shipping_method) docreate(@@ -336,11 +336,11 @@ describe 'Checkout steps (address and delivery)' dochoose "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']").clickwait_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']").clickwait_for_turbo@@ -358,8 +358,8 @@ describe 'Checkout steps (address and delivery)' dochoose "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.codeclick_on 'Apply'@@ -370,7 +370,7 @@ describe 'Checkout steps (address and delivery)' doexpect(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']").clickwithin('div.summary-content') doexpect(page).to have_content('Free')@@ -398,7 +398,7 @@ describe 'Checkout steps (address and delivery)' doexpect(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']").clickexpect(page).to have_content('Payment')end@@ -409,7 +409,7 @@ describe 'Checkout steps (address and delivery)' dolet(: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))endbefore do@@ -420,13 +420,13 @@ describe 'Checkout steps (address and delivery)' dovisit "/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']").clickclick_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)' dovisit "/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']").clickclick_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')