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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreemodule Rulesclass Country < PromotionRulepreference :country_id, :integer+ preference :country_iso, :string # Alternative way to configure the ruledef applicable?(promotable)promotable.is_a?(Spree::Order)enddef 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))+ falseendendenddiff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex 4642023cbf..308ac6bfab 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -114,6 +114,8 @@ module Spreeafter_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).executeenddiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 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 countryemail: Emailempty: Emptyempty_cart: Empty Cartdiff --git a/core/spec/models/spree/promotion/rules/country_spec.rb b/core/spec/models/spree/promotion/rules/country_spec.rbindex 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 dolet(:country) { create(:country) }let(:other_country) { create(:country) }- context 'preferred country is set' do+ context 'preferred country_id is set' dobefore { rule.preferred_country_id = country.id }it 'is eligible for correct country' doallow(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)endit 'is not eligible for incorrect country' doallow(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')])endendcontext 'preferred country is not set' doit 'is eligible for default country' doallow(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)endit 'is not eligible for incorrect country' doallow(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')])endendend