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.

Auto-Remove items from cart when not available in new currency (#12104)

Damian Legawiec5f8cac0a0ade
diff --git a/core/app/models/spree/order/currency_updater.rb b/core/app/models/spree/order/currency_updater.rb
index 5b39e92037..28ac445f1f 100644
--- a/core/app/models/spree/order/currency_updater.rb
+++ b/core/app/models/spree/order/currency_updater.rb
@@ -26,10 +26,10 @@ module Spree
def update_line_item_price!(line_item)
price = price_from_line_item(line_item)
- if price
+ if price&.currency && price.amount
line_item.update!(currency: price.currency, price: price.amount)
else
- raise "no #{currency} price found for #{line_item.product.name} (#{line_item.variant.sku})"
+ line_item.destroy
end
end
end
diff --git a/core/spec/models/spree/order/currency_updater_spec.rb b/core/spec/models/spree/order/currency_updater_spec.rb
index a594b163fb..f15ca8419e 100644
--- a/core/spec/models/spree/order/currency_updater_spec.rb
+++ b/core/spec/models/spree/order/currency_updater_spec.rb
@@ -19,13 +19,17 @@ describe Spree::Order, type: :model do
expect { line_item.order.update!(currency: 'EUR') }.to change { line_item.reload.amount }.to(8)
end
- it 'fails to change the order currency when no prices are available in that currency' do
- expect { line_item.order.update!(currency: 'GBP') }.to raise_error("no GBP price found for #{line_item.product.name} (#{line_item.variant.sku})")
- end
-
it 'calculates the item total in the order.currency' do
expect { line_item.order.update!(currency: 'EUR') }.to change { line_item.order.item_total }.to(8)
end
+
+ context 'when there is a price with nil amount' do
+ let!(:euro_price) { create(:price, variant: line_item.variant, amount: nil, currency: 'EUR') }
+
+ it 'destroys the line item when we switch to that price\'s currency' do
+ expect { line_item.order.update!(currency: 'EUR') }.to change(Spree::LineItem, :count).by(-1)
+ end
+ end
end
end
end
diff --git a/core/spec/services/spree/cart/change_currency_spec.rb b/core/spec/services/spree/cart/change_currency_spec.rb
index 3c8fd65169..4f1c2a724f 100644
--- a/core/spec/services/spree/cart/change_currency_spec.rb
+++ b/core/spec/services/spree/cart/change_currency_spec.rb
@@ -37,15 +37,6 @@ module Spree
end
end
end
-
- context 'when product does not have a price in given currency' do
- it 'returns failure' do
- expect(subject).to be_failure
- order.reload
- expect(order.currency).to eq('USD')
- expect(order.line_items.first.currency).to eq('USD')
- end
- end
end
context 'when switching to an unsupported currency' do
@@ -58,5 +49,15 @@ module Spree
expect(order.line_items.first.currency).to eq('USD')
end
end
+
+ context 'when there are items that are not available in the new currency' do
+ let(:new_currency) { 'EUR' }
+
+ it 'removes them from the Cart' do
+ expect(order.line_items).not_to be_empty
+ expect(subject).to be_success
+ expect(Spree::Order.find(order.id).line_items).to be_empty
+ end
+ end
end
end