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.

Use option value variant ids as eligible values for the Option Value promo rule (#12208)

Łukasz Adamczyk66039444b3af
diff --git a/core/app/models/spree/promotion/rules/option_value.rb b/core/app/models/spree/promotion/rules/option_value.rb
index c5932eb3df..d14d0fb708 100644
--- a/core/app/models/spree/promotion/rules/option_value.rb
+++ b/core/app/models/spree/promotion/rules/option_value.rb
@@ -1,23 +1,12 @@
module Spree
class Promotion
module Rules
- module OptionValueWithNumerificationSupport
- def preferred_eligible_values
- values = super || {}
- Hash[values.keys.zip(
- values.values.map do |v|
- (v.is_a?(Array) ? v : v.split(','))
- end
- )]
- end
- end
-
class OptionValue < PromotionRule
- prepend OptionValueWithNumerificationSupport
-
MATCH_POLICIES = %w(any)
preference :match_policy, :string, default: MATCH_POLICIES.first
- preference :eligible_values, :hash
+ preference :eligible_values, :array, default: [], parse_on_set: lambda { |values|
+ values.flat_map { |v| v.to_s.split(',').compact_blank.map(&:strip) }
+ }
def applicable?(promotable)
promotable.is_a?(Spree::Order)
@@ -31,12 +20,8 @@ module Spree
end
def actionable?(line_item)
- product_id = line_item.product_id
- option_values_ids = line_item.variant.option_value_ids
- eligible_product_ids = preferred_eligible_values.keys
- eligible_value_ids = preferred_eligible_values[product_id]
-
- eligible_product_ids.include?(product_id) && (eligible_value_ids & option_values_ids).present?
+ option_value_variant_ids = line_item.variant.option_value_variant_ids.map(&:to_s)
+ (preferred_eligible_values & option_value_variant_ids).any?
end
end
end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 308ac6bfab..c375fa94fc 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -59,6 +59,7 @@ module Spree
has_many :variants, through: :products, class_name: 'Spree::Variant', source: :variants_including_master
has_many :stock_items, through: :variants, class_name: 'Spree::StockItem'
has_many :inventory_units, through: :variants, class_name: 'Spree::InventoryUnit'
+ has_many :option_value_variants, through: :variants, class_name: 'Spree::OptionValueVariant'
has_many :customer_returns, class_name: 'Spree::CustomerReturn', inverse_of: :store
has_many :store_credits, class_name: 'Spree::StoreCredit'
diff --git a/core/db/migrate/20241104083457_migrate_spree_promotion_rules_option_value_eligible_values.rb b/core/db/migrate/20241104083457_migrate_spree_promotion_rules_option_value_eligible_values.rb
new file mode 100644
index 0000000000..235ee284b4
--- /dev/null
+++ b/core/db/migrate/20241104083457_migrate_spree_promotion_rules_option_value_eligible_values.rb
@@ -0,0 +1,16 @@
+class MigrateSpreePromotionRulesOptionValueEligibleValues < ActiveRecord::Migration[6.1]
+ def change
+ Spree::Promotion::Rules::OptionValue.find_each do |option_value|
+ new_eligible_values = option_value.preferred_eligible_values.flat_map do |product_id, option_value_ids|
+ value_ids = option_value_ids.is_a?(String) ? option_value_ids.split(',') : option_value_ids
+
+ Spree::OptionValueVariant.
+ joins(:variant).
+ where(option_value_id: value_ids, "#{Spree::Variant.table_name}.product_id" => product_id).
+ pluck(:id)
+ end
+
+ option_value.update!(preferred_eligible_values: new_eligible_values)
+ end
+ end
+end
diff --git a/core/lib/spree/core/preferences/preferable_class_methods.rb b/core/lib/spree/core/preferences/preferable_class_methods.rb
index b5133cfbb5..12c1b03548 100644
--- a/core/lib/spree/core/preferences/preferable_class_methods.rb
+++ b/core/lib/spree/core/preferences/preferable_class_methods.rb
@@ -2,11 +2,12 @@ module Spree::Preferences
module PreferableClassMethods
def preference(name, type, *args)
options = args.extract_options!
- options.assert_valid_keys(:default, :deprecated, :nullable)
+ options.assert_valid_keys(:default, :deprecated, :nullable, :parse_on_set)
default = options[:default]
default = -> { options[:default] } unless default.is_a?(Proc)
deprecated = options[:deprecated]
nullable = options[:nullable]
+ parse_on_set = options[:parse_on_set]
# cache_key will be nil for new objects, then if we check if there
# is a pending preference before going to default
@@ -17,6 +18,7 @@ module Spree::Preferences
end
define_method preference_setter_method(name) do |value|
+ value = parse_on_set.call(value) if parse_on_set.is_a?(Proc)
value = convert_preference_value(value, type, nullable: nullable)
preferences[name] = value
diff --git a/core/spec/models/spree/promotion/rules/option_value_spec.rb b/core/spec/models/spree/promotion/rules/option_value_spec.rb
index 6356fd31dc..1a16596f1c 100644
--- a/core/spec/models/spree/promotion/rules/option_value_spec.rb
+++ b/core/spec/models/spree/promotion/rules/option_value_spec.rb
@@ -3,12 +3,17 @@ require 'spec_helper'
describe Spree::Promotion::Rules::OptionValue do
let(:rule) { described_class.new }
- describe '#preferred_eligible_values' do
- subject { rule.preferred_eligible_values }
+ describe 'setting eligible values' do
+ let(:option_value_variant_ids) do
+ [
+ 'abc-123',
+ 'def-123, def-456,ghj-789 , '
+ ]
+ end
- it 'assigns a nicely formatted hash' do
- rule.preferred_eligible_values = Hash['5' => '1,2', '6' => '1']
- expect(subject).to eq Hash['5' => ['1', '2'], '6' => ['1']]
+ it 'parses ids' do
+ rule.preferred_eligible_values = option_value_variant_ids
+ expect(rule.preferred_eligible_values).to eq(['abc-123', 'def-123', 'def-456', 'ghj-789'])
end
end
@@ -37,9 +42,7 @@ describe Spree::Promotion::Rules::OptionValue do
context 'when there are any applicable line items' do
before do
- rule.preferred_eligible_values = Hash[variant.product.id => [
- variant.option_values.pluck(:id).first
- ]]
+ rule.preferred_eligible_values = [variant.option_value_variant_ids.first, 1233542]
end
it { is_expected.to be true }
@@ -47,7 +50,7 @@ describe Spree::Promotion::Rules::OptionValue do
context 'when there are no applicable line items' do
before do
- rule.preferred_eligible_values = Hash[99 => [99]]
+ rule.preferred_eligible_values = [123235234, 4531236]
end
it { is_expected.to be false }
@@ -80,28 +83,41 @@ describe Spree::Promotion::Rules::OptionValue do
before do
line_item.variant.option_values << option_value_blue
- rule.preferred_eligible_values = Hash[product_id => option_value_ids]
+ rule.preferred_eligible_values = option_value_variant_ids
end
context 'when the line item has the correct product' do
let(:product_id) { line_item.product.id }
context 'when all of the option values match' do
- let(:option_value_ids) { [option_value_blue.id] }
+ let(:option_value_variant_ids) do
+ [
+ line_item.variant.option_value_variants.reload.find_by(option_value: option_value_blue).id
+ ]
+ end
it { is_expected.to be true }
end
context 'when not all of the option values match' do
- let(:option_value_ids) { [option_value_blue.id, option_value_medium.id] }
+ let(:option_value_variant_ids) do
+ [
+ line_item.variant.option_value_variants.reload.find_by(option_value: option_value_blue).id,
+ 12356342
+ ]
+ end
it { is_expected.to be true }
end
end
context "when the line item's product doesn't match" do
- let(:product_id) { 99 }
- let(:option_value_ids) { [99] }
+ let(:option_value_variant_ids) do
+ [
+ 12312423,
+ 45823843
+ ]
+ end
it { is_expected.to be false }
end