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.

Added `Currency` promotion rule

Damian Legawiecc4ff11596762
diff --git a/admin/app/views/spree/admin/promotions/rules/_currency.html.erb b/admin/app/views/spree/admin/promotions/rules/_currency.html.erb
new file mode 100644
index 0000000000..feab4ca885
--- /dev/null
+++ b/admin/app/views/spree/admin/promotions/rules/_currency.html.erb
@@ -0,0 +1,13 @@
+
+<% active_option ||= current_store.default_currency %>
+<% active_option = rule_form.object.preferred_currency if rule_form.object.persisted? && rule_form.object.respond_to?(:preferred_currency) %>
+
+<div class="form-group">
+ <%= select_tag "#{prefix}[preferred_currency]",
+ currency_options_for_select(
+ active_option,
+ current_store.supported_currencies.split(',')
+ ),
+ { data: { controller: 'autocomplete-select' } }
+ %>
+</div>
diff --git a/core/app/models/spree/promotion/rules/currency.rb b/core/app/models/spree/promotion/rules/currency.rb
new file mode 100644
index 0000000000..bb58652fab
--- /dev/null
+++ b/core/app/models/spree/promotion/rules/currency.rb
@@ -0,0 +1,21 @@
+# A rule to limit a promotion based on order currency.
+module Spree
+ class Promotion
+ module Rules
+ class Currency < PromotionRule
+ preference :currency, :string
+
+ def applicable?(promotable)
+ promotable.is_a?(Spree::Order)
+ end
+
+ def eligible?(order, options = {})
+ return true if order.currency == preferred_currency
+
+ eligibility_errors.add(:base, eligibility_error_message(:wrong_currency))
+ false
+ end
+ end
+ end
+ end
+end
diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rb
index 4fdf537562..c7a4496d72 100644
--- a/core/lib/spree/core/engine.rb
+++ b/core/lib/spree/core/engine.rb
@@ -120,6 +120,7 @@ module Spree
]
Rails.application.config.spree.promotions.rules.concat [
+ Spree::Promotion::Rules::Currency,
Spree::Promotion::Rules::ItemTotal,
Spree::Promotion::Rules::Product,
Spree::Promotion::Rules::User,
diff --git a/core/spec/models/spree/promotion/rules/currency_spec.rb b/core/spec/models/spree/promotion/rules/currency_spec.rb
new file mode 100644
index 0000000000..f2348bd1d1
--- /dev/null
+++ b/core/spec/models/spree/promotion/rules/currency_spec.rb
@@ -0,0 +1,39 @@
+require 'spec_helper'
+
+describe Spree::Promotion::Rules::Currency, type: :model do
+ let(:store) { Spree::Store.default }
+ let(:rule) { described_class.new }
+ let(:order) { create(:order, store: store) }
+
+ context 'preferred currency is set' do
+ before { rule.preferred_currency = 'EUR' }
+
+ it 'is eligible for correct currency' do
+ order.currency = 'EUR'
+ expect(rule).to be_eligible(order)
+ expect(rule.eligibility_errors).to be_empty
+ end
+
+ it 'is not eligible for incorrect currency' do
+ order.currency = 'USD'
+ expect(rule).not_to be_eligible(order)
+
+ expect(rule.eligibility_errors.count).to eq(1)
+ expect(rule.eligibility_errors.to_hash[:base]).to eq([Spree.t('eligibility_errors.messages.wrong_currency')])
+ end
+ end
+
+ describe '#applicable?' do
+ subject { rule.applicable?(promotable) }
+
+ context 'when promotable is an order' do
+ let(:promotable) { Spree::Order.new }
+ it { is_expected.to be true }
+ end
+
+ context 'when promotable is not an order' do
+ let(:promotable) { Spree::LineItem.new }
+ it { is_expected.to be false }
+ end
+ end
+end