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 setting delivery method costs in different currencies (#14798)

Kacper Mękarski63dd7056b6e0
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb
index e6b29eda37..acf5cfe4d8 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/delivery_methods_controller_spec.rb
@@ -234,6 +234,22 @@ RSpec.describe Spree::Api::V3::Admin::DeliveryMethodsController, type: :controll
expect(delivery_method.calculator.preferred_amount).to eq(99)
end
+ it 'stores per-currency amounts' do
+ patch :update, params: {
+ id: delivery_method.prefixed_id,
+ calculator_type: 'flat_rate',
+ calculator_preferences: { amount: 20, currency: 'USD', amounts: { 'EUR' => '15.0' } }
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['calculator_preferences']['amounts']).to eq('EUR' => '15.0')
+
+ calculator = delivery_method.reload.calculator
+ expect(calculator.preferred_amounts).to eq('EUR' => '15.0')
+ expect(calculator.amount_for('EUR')).to eq(15.0)
+ expect(calculator.amount_for('USD')).to eq(20)
+ end
+
# The dashboard sheet saves basics and conditions together.
it 'saves nested eligibility rules alongside the method' do
product = create(:product)
diff --git a/spree/core/lib/spree/core/preferences/preferable.rb b/spree/core/lib/spree/core/preferences/preferable.rb
index 81e9abae8f..d80f3e7ec5 100644
--- a/spree/core/lib/spree/core/preferences/preferable.rb
+++ b/spree/core/lib/spree/core/preferences/preferable.rb
@@ -195,6 +195,8 @@ module Spree::Preferences::Preferable
case value.class.to_s
when 'Hash'
value
+ when 'ActionController::Parameters'
+ value.to_h
when 'String'
# only works with hashes whose keys are strings
JSON.parse value.gsub('=>', ':')
diff --git a/spree/core/spec/models/spree/preferences/preferable_spec.rb b/spree/core/spec/models/spree/preferences/preferable_spec.rb
index 40cd87eed1..107066400d 100644
--- a/spree/core/spec/models/spree/preferences/preferable_spec.rb
+++ b/spree/core/spec/models/spree/preferences/preferable_spec.rb
@@ -332,6 +332,21 @@ describe Spree::Preferences::Preferable, type: :model do
it 'with single array' do
expect { @a.set_preference(:is_hash, ['key']) }.to raise_error(ArgumentError)
end
+
+ it 'with permitted strong parameters' do
+ parameters = ActionController::Parameters.new(amounts: { 'EUR' => '15.0' }).permit(amounts: {})
+
+ @a.set_preference(:is_hash, parameters[:amounts])
+
+ expect(@a.preferences[:is_hash]).to eq('EUR' => '15.0')
+ expect(@a.preferences[:is_hash]).to be_is_a(Hash)
+ end
+
+ it 'with unpermitted strong parameters' do
+ parameters = ActionController::Parameters.new('EUR' => '15.0')
+
+ expect { @a.set_preference(:is_hash, parameters) }.to raise_error(ActionController::UnfilteredParameters)
+ end
end
# Deliberately not coerced to a Date: a date's meaning depends on the