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.

Add the country_iso preference to Country promotion rule (#12199)

Łukasz Adamczyk7fef1f616939
diff --git a/core/app/models/spree/promotion/rules/country.rb b/core/app/models/spree/promotion/rules/country.rb
index 247725926d..22141a940e 100644
--- a/core/app/models/spree/promotion/rules/country.rb
+++ b/core/app/models/spree/promotion/rules/country.rb
@@ -4,18 +4,36 @@ module Spree
module Rules
class Country < PromotionRule
preference :country_id, :integer
+ preference :country_iso, :string # Alternative way to configure the rule
def applicable?(promotable)
promotable.is_a?(Spree::Order)
end
def eligible?(order, options = {})
- country_id = options[:country_id] || order.ship_address.try(:country_id)
- unless country_id.eql?(preferred_country_id || order.store.default_country_id)
- eligibility_errors.add(:base, eligibility_error_message(:wrong_country))
+ if preferred_country_iso.present?
+ validate_eligibility_by_country_iso(order)
+ else
+ validate_eligibility_by_country_id(order, options)
end
+ end
+
+ private
+
+ def validate_eligibility_by_country_id(order, options)
+ country_id = options[:country_id] || order.ship_address.try(:country_id)
+ return true if country_id == (preferred_country_id || order.store.default_country_id)
+
+ eligibility_errors.add(:base, eligibility_error_message(:wrong_country))
+ false
+ end
+
+ def validate_eligibility_by_country_iso(order)
+ country_iso = order.ship_address&.country_iso
+ return true if country_iso == (preferred_country_iso || order.store.default_country_iso)
- eligibility_errors.empty?
+ eligibility_errors.add(:base, eligibility_error_message(:wrong_country))
+ false
end
end
end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 4642023cbf..308ac6bfab 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -114,6 +114,8 @@ module Spree
after_commit :clear_cache
+ delegate :iso, to: :default_country, prefix: true, allow_nil: true
+
def self.current(url = nil)
Spree::Dependencies.current_store_finder.constantize.new(url: url).execute
end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index f842212175..b29b399712 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -807,6 +807,7 @@ en:
no_user_or_email_specified: You need to login or provide your email before applying this coupon code.
no_user_specified: You need to login before applying this coupon code.
not_first_order: This coupon code can only be applied to your first order.
+ wrong_country: This coupon code is not eligible in your country
email: Email
empty: Empty
empty_cart: Empty Cart
diff --git a/core/spec/models/spree/promotion/rules/country_spec.rb b/core/spec/models/spree/promotion/rules/country_spec.rb
index 6052f1233d..4315504d04 100644
--- a/core/spec/models/spree/promotion/rules/country_spec.rb
+++ b/core/spec/models/spree/promotion/rules/country_spec.rb
@@ -7,29 +7,65 @@ describe Spree::Promotion::Rules::Country, type: :model do
let(:country) { create(:country) }
let(:other_country) { create(:country) }
- context 'preferred country is set' do
+ context 'preferred country_id is set' do
before { rule.preferred_country_id = country.id }
it 'is eligible for correct country' do
allow(order).to receive_message_chain(:ship_address, :country_id) { country.id }
+ allow(order).to receive_message_chain(:ship_address, :country_iso) { country.iso }
+
expect(rule).to be_eligible(order)
end
it 'is not eligible for incorrect country' do
allow(order).to receive_message_chain(:ship_address, :country_id) { other_country.id }
+ allow(order).to receive_message_chain(:ship_address, :country_iso) { other_country.iso }
+
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_country')])
+ end
+ end
+
+ context 'preferred country_iso is set' do
+ before { rule.preferred_country_iso = country.iso }
+
+ it 'is eligible for correct country' do
+ allow(order).to receive_message_chain(:ship_address, :country_id) { country.id }
+ allow(order).to receive_message_chain(:ship_address, :country_iso) { country.iso }
+
+ expect(rule).to be_eligible(order)
+ expect(rule.eligibility_errors).to be_empty
+ end
+
+ it 'is not eligible for incorrect country' do
+ allow(order).to receive_message_chain(:ship_address, :country_id) { other_country.id }
+ allow(order).to receive_message_chain(:ship_address, :country_iso) { other_country.iso }
+
+ 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_country')])
end
end
context 'preferred country is not set' do
it 'is eligible for default country' do
allow(order).to receive_message_chain(:ship_address, :country_id) { other_country.id }
+ allow(order).to receive_message_chain(:ship_address, :country_iso) { other_country.iso }
+
expect(rule).to be_eligible(order)
end
it 'is not eligible for incorrect country' do
allow(order).to receive_message_chain(:ship_address, :country_id) { country.id }
+ allow(order).to receive_message_chain(:ship_address, :country_iso) { country.iso }
+
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_country')])
end
end
end