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
Keep a promotion inside its own store's catalog
Damian Legawiecfcae3496bc90
diff --git a/spree/core/app/models/spree/promotion/actions/create_line_items.rb b/spree/core/app/models/spree/promotion/actions/create_line_items.rbindex 4832f95387..7494e000c7 100644--- a/spree/core/app/models/spree/promotion/actions/create_line_items.rb+++ b/spree/core/app/models/spree/promotion/actions/create_line_items.rb@@ -98,6 +98,12 @@ module Spreeprivate+ # An action built before its promotion is attached has no store to+ # read, so it resolves nothing rather than the whole catalog.+ def promotion_variants_scope+ promotion&.store&.variants || Spree::Variant.none+ end+# Handles the creation, updating, and pruning of promotion action# line items. The submitted list is the *desired* set — variants# not present are deleted, ones that are get upserted. Accepts@@ -109,9 +115,16 @@ module Spreerows = promotion_action_line_items_attributes.is_a?(Hash) ? promotion_action_line_items_attributes.values : promotion_action_line_items_attributesrows = rows.map { |row| row.respond_to?(:to_h) ? row.to_h.with_indifferent_access : row.with_indifferent_access }- rows = rows.map do |row|+ # Resolved through the promotion's own store, so a variant from+ # another store's catalog resolves to nothing and is dropped rather+ # than gifted by a promotion that cannot sell it. An unresolvable id+ # is dropped for the same reason a nil one always was — the upsert+ # would otherwise write a row with no variant.+ rows = rows.filter_map do |row|variant_id = row['variant_id']- variant_id = Spree::Variant.find_by_param(variant_id)&.id if Spree::PrefixedId.prefixed_id?(variant_id)+ variant_id = promotion_variants_scope.find_by_param(variant_id)&.id if Spree::PrefixedId.prefixed_id?(variant_id)+ next if variant_id.blank?+row.merge('variant_id' => variant_id)enddiff --git a/spree/core/app/models/spree/promotion/rules/category.rb b/spree/core/app/models/spree/promotion/rules/category.rbindex 66ab0f2afb..b971d4a968 100644--- a/spree/core/app/models/spree/promotion/rules/category.rb+++ b/spree/core/app/models/spree/promotion/rules/category.rb@@ -25,9 +25,13 @@ module Spree# Decode prefixed IDs before delegating to the generated setter (direct# calls bypass PrefixedId's assign_attributes auto-resolver).+ #+ # Resolved through the promotion's own store, so an id belonging to+ # another store raises rather than quietly linking — the promotion+ # would otherwise discount against a catalog its store cannot see.def category_ids=(ids)super(Array(ids).map do |id|- Spree::PrefixedId.prefixed_id?(id) ? Spree::Category.find_by_param!(id).id : id+ Spree::PrefixedId.prefixed_id?(id) ? promotion_categories_scope.find_by_param!(id).id : idend)end@@ -134,6 +138,12 @@ module Spreeprivate+ # A rule built before its promotion is attached has no store to read,+ # so it resolves nothing rather than falling back to every category.+ def promotion_categories_scope+ promotion&.store&.categories || Spree::Category.none+ end+# IDs of categories in rule including all their childrendef eligible_category_ids_including_children@eligible_category_ids_including_children ||= begindiff --git a/spree/core/app/models/spree/promotion/rules/customer_group.rb b/spree/core/app/models/spree/promotion/rules/customer_group.rbindex fd6bb0eb4d..6cc55bcdfd 100644--- a/spree/core/app/models/spree/promotion/rules/customer_group.rb+++ b/spree/core/app/models/spree/promotion/rules/customer_group.rb@@ -5,7 +5,15 @@ module Spree# Stored as raw IDs. Accepts prefixed IDs (`cg_…`) from API# callers and decodes them on write so eligibility checks can# compare against raw `customer_group_id` rows directly.- preference :customer_group_ids, :array, default: [], parse_on_set: normalize_id_preference(klass: Spree::CustomerGroup)+ #+ # Scoped to the promotion's own store: groups are store-owned, so an+ # id from elsewhere must raise rather than link a promotion to an+ # audience its store cannot see.+ preference :customer_group_ids, :array, default: [],+ parse_on_set: normalize_id_preference(+ klass: Spree::CustomerGroup,+ scope: ->(rule) { rule.promotion&.store&.customer_groups || Spree::CustomerGroup.none }+ )def applicable?(promotable)promotable.is_a?(Spree::Order) || promotable.is_a?(Spree::Cart)diff --git a/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rb b/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rbindex 29d5dd08b8..3305ccfe76 100644--- a/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rb+++ b/spree/core/spec/models/spree/promotion/actions/create_line_items_spec.rb@@ -136,4 +136,19 @@ describe Spree::Promotion::Actions::CreateLineItems, type: :model doendendend++ # A gift the promotion's store cannot sell is dropped rather than written+ # as a row with no variant.+ context 'when given a variant from another store' do+ let(:foreign_variant) { create(:product, store: create(:store)).default_variant }++ it 'drops it' do+ action.promotion_action_line_items_attributes = [+ { 'variant_id' => foreign_variant.prefixed_id, 'quantity' => 1 }+ ]+ action.save!++ expect(action.promotion_action_line_items.reload).to be_empty+ end+ endenddiff --git a/spree/core/spec/models/spree/promotion/rules/category_spec.rb b/spree/core/spec/models/spree/promotion/rules/category_spec.rbindex cde03a8ab1..6ebdad9834 100644--- a/spree/core/spec/models/spree/promotion/rules/category_spec.rb+++ b/spree/core/spec/models/spree/promotion/rules/category_spec.rb@@ -127,4 +127,14 @@ describe Spree::Promotion::Rules::Category, type: :model doexpect(rule.taxons).to match_array(taxons)endend++ # A promotion must not discount against a catalog its own store cannot see.+ context 'when given a category from another store' do+ let(:foreign_category) { create(:category, store: create(:store)) }++ it 'rejects the foreign ID' do+ expect { rule.category_ids = [foreign_category.prefixed_id] }.+ to raise_error(ActiveRecord::RecordNotFound)+ end+ endenddiff --git a/spree/core/spec/models/spree/promotion/rules/customer_group_spec.rb b/spree/core/spec/models/spree/promotion/rules/customer_group_spec.rbindex 851bf300bb..5468ffff41 100644--- a/spree/core/spec/models/spree/promotion/rules/customer_group_spec.rb+++ b/spree/core/spec/models/spree/promotion/rules/customer_group_spec.rb@@ -2,7 +2,10 @@ require 'spec_helper'describe Spree::Promotion::Rules::CustomerGroup, type: :model dolet(:store) { @default_store }- let(:rule) { Spree::Promotion::Rules::CustomerGroup.new }+ let(:promotion) { create(:promotion, store: store) }+ # Attached to its promotion, because the rule resolves group ids through+ # that promotion's store.+ let(:rule) { described_class.new(promotion: promotion) }let(:customer_group) { create(:customer_group, store: store) }let(:other_customer_group) { create(:customer_group, store: store) }let(:user) { create(:user) }@@ -83,4 +86,15 @@ describe Spree::Promotion::Rules::CustomerGroup, type: :model doendendend++ # Groups are store-owned, so a promotion must not target an audience+ # belonging to another store.+ context 'when given a customer group from another store' do+ let(:foreign_group) { create(:customer_group, store: create(:store)) }++ it 'rejects the foreign ID' do+ expect { rule.preferred_customer_group_ids = [foreign_group.prefixed_id] }.+ to raise_error(ActiveRecord::RecordNotFound)+ end+ endend