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.

[SD-994, NUT-200] Shipping summary label issue (#10563)

Kacper Mękarski92530ca9097b
diff --git a/core/app/models/spree/adjustment.rb b/core/app/models/spree/adjustment.rb
index 37ada324a0..89731272a6 100644
--- a/core/app/models/spree/adjustment.rb
+++ b/core/app/models/spree/adjustment.rb
@@ -27,6 +27,7 @@ module Spree
belongs_to :source
end
belongs_to :order, class_name: 'Spree::Order', inverse_of: :all_adjustments
+ belongs_to :promotion_action, class_name: 'Spree::PromotionAction', foreign_key: :source_id, optional: true # created only for has_free_shipping?
validates :adjustable, :order, :label, presence: true
validates :amount, numericality: true
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 56f9337fb6..53c8e4e6df 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -649,10 +649,10 @@ module Spree
end
def has_free_shipping?
- promotions.
- joins(:promotion_actions).
- where(spree_promotion_actions: { type: 'Spree::Promotion::Actions::FreeShipping' }).
- exists?
+ shipment_adjustments.
+ joins(:promotion_action).
+ where(spree_adjustments: { eligible: true, source_type: 'Spree::PromotionAction' },
+ spree_promotion_actions: { type: 'Spree::Promotion::Actions::FreeShipping' }).exists?
end
private
diff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rb
index dfb5948804..a1fdc8a3c6 100644
--- a/core/lib/spree/testing_support/factories/promotion_factory.rb
+++ b/core/lib/spree/testing_support/factories/promotion_factory.rb
@@ -1,4 +1,21 @@
FactoryBot.define do
+ trait :with_item_total_rule do
+ transient do
+ item_total_threshold_amount { 10 }
+ end
+
+ after(:create) do |promotion, evaluator|
+ rule = Spree::Promotion::Rules::ItemTotal.create!(
+ preferred_operator_min: 'gte',
+ preferred_operator_max: 'lte',
+ preferred_amount_min: evaluator.item_total_threshold_amount,
+ preferred_amount_max: evaluator.item_total_threshold_amount + 100
+ )
+ promotion.rules << rule
+ promotion.save!
+ end
+ end
+
factory :promotion, class: Spree::Promotion do
name { 'Promo' }
@@ -37,24 +54,8 @@ FactoryBot.define do
promotion.save!
end
end
- factory :promotion_with_order_adjustment, traits: [:with_order_adjustment]
-
- trait :with_item_total_rule do
- transient do
- item_total_threshold_amount { 10 }
- end
- after(:create) do |promotion, evaluator|
- rule = Spree::Promotion::Rules::ItemTotal.create!(
- preferred_operator_min: 'gte',
- preferred_operator_max: 'lte',
- preferred_amount_min: evaluator.item_total_threshold_amount,
- preferred_amount_max: evaluator.item_total_threshold_amount + 100
- )
- promotion.rules << rule
- promotion.save!
- end
- end
+ factory :promotion_with_order_adjustment, traits: [:with_order_adjustment]
factory :promotion_with_item_total_rule, traits: [:with_item_total_rule]
end
@@ -66,5 +67,7 @@ FactoryBot.define do
action.promotion = promotion
action.save
end
+
+ factory :free_shipping_promotion_with_item_total_rule, traits: [:with_item_total_rule]
end
end
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 5a37e4ac77..21411bbe4e 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -1347,4 +1347,45 @@ describe Spree::Order, type: :model do
end
end
end
+
+ describe '#has_free_shipping?' do
+ subject { order.has_free_shipping? }
+
+ let(:order) { create(:order_with_line_items, line_items_count: line_items_count) }
+ let(:line_items_count) { 10 }
+
+ context 'when promotion is applied' do
+ let(:free_shipping_promotion) { create(:free_shipping_promotion, code: 'freeship') }
+
+ before do
+ order.coupon_code = free_shipping_promotion.code
+ Spree::PromotionHandler::Coupon.new(order).apply
+ end
+
+ it { is_expected.to be true }
+
+ context 'when free shipping promotion has item total rule' do
+ let(:free_shipping_promotion) do
+ create(:free_shipping_promotion_with_item_total_rule,
+ code: 'freeship',
+ starts_at: 1.day.ago,
+ expires_at: 1.day.from_now)
+ end
+
+ context 'when order total is in defined range' do
+ it { is_expected.to be true }
+ end
+
+ context 'when order total is not in defined range' do
+ let(:line_items_count) { 15 }
+
+ it { is_expected.to be false }
+ end
+ end
+ end
+
+ context 'when promotion is not applied' do
+ it { is_expected.to be false }
+ end
+ end
end
diff --git a/frontend/spec/features/free_shipping_promotions_spec.rb b/frontend/spec/features/free_shipping_promotions_spec.rb
index 1a1636fd99..1e27378637 100644
--- a/frontend/spec/features/free_shipping_promotions_spec.rb
+++ b/frontend/spec/features/free_shipping_promotions_spec.rb
@@ -3,7 +3,6 @@ require 'spec_helper'
describe 'Free shipping promotions', type: :feature, js: true do
let!(:country) { create(:country, name: 'United States of America', states_required: true) }
let!(:state) { create(:state, name: 'Alabama', country: country) }
- let!(:mug) { create(:product, name: 'RoR Mug', price: 20) }
before do
create(:zone)
@@ -13,10 +12,6 @@ describe 'Free shipping promotions', type: :feature, js: true do
create(:check_payment_method)
- promotion = Spree::Promotion.create!(name: 'Free Shipping',
- starts_at: 1.day.ago,
- expires_at: 1.day.from_now)
-
action = Spree::Promotion::Actions::FreeShipping.new
action.promotion = promotion
action.save
@@ -25,6 +20,14 @@ describe 'Free shipping promotions', type: :feature, js: true do
end
context 'free shipping promotion automatically applied' do
+ let!(:mug) { create(:product, name: 'RoR Mug', price: 20) }
+ let(:promotion) do
+ create(:promotion,
+ name: 'Free Shipping',
+ starts_at: 1.day.ago,
+ expires_at: 1.day.from_now)
+ end
+
include_context 'proceed to payment step'
# Regression test for #4428
@@ -35,4 +38,32 @@ describe 'Free shipping promotions', type: :feature, js: true do
end
end
end
+
+ context 'when free shipping promotion applies for order total in defined range' do
+ let(:promotion) do
+ create(:free_shipping_promotion_with_item_total_rule,
+ name: 'Free Shipping',
+ starts_at: 1.day.ago,
+ expires_at: 1.day.from_now)
+ end
+
+ include_context 'proceed to payment step'
+
+ context 'when order total is less than defined range' do
+ let!(:mug) { create(:product, name: 'RoR Mug', price: 5) }
+
+ it 'does not apply the free shipping promotion' do
+ page.has_text? 'SHIPPING: $10.00'
+ page.has_text? 'Promotion (Free Shipping): -$10.00'
+ end
+ end
+
+ context 'when order total is greater than defined range' do
+ let!(:mug) { create(:product, name: 'RoR Mug', price: 60) }
+
+ it 'applies the free shipping promotion' do
+ page.has_text? 'SHIPPING: FREE'
+ end
+ end
+ end
end