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.

Sort OptionValues by position in filter metadata (#11549)

Aleksandar Petrusheva6c18474d7bd
diff --git a/core/app/finders/spree/option_values/find_available.rb b/core/app/finders/spree/option_values/find_available.rb
index 7ee16c1977..034119bd22 100644
--- a/core/app/finders/spree/option_values/find_available.rb
+++ b/core/app/finders/spree/option_values/find_available.rb
@@ -9,12 +9,20 @@ module Spree
end
def execute
- find_available(scope, products_scope)
+ find_available(scope, products_scope).select(select_args).order(order_args)
end
private
attr_reader :scope, :products_scope
+
+ def select_args
+ "#{OptionValue.table_name}.*, #{OptionType.table_name}.position AS option_type_position"
+ end
+
+ def order_args
+ "option_type_position, #{OptionValue.table_name}.position"
+ end
end
end
end
diff --git a/core/spec/finders/spree/option_values/find_available_spec.rb b/core/spec/finders/spree/option_values/find_available_spec.rb
index 46da53466b..418a44c7f8 100644
--- a/core/spec/finders/spree/option_values/find_available_spec.rb
+++ b/core/spec/finders/spree/option_values/find_available_spec.rb
@@ -68,6 +68,35 @@ module Spree
)
end
end
+
+ context 'ordering' do
+ let!(:white_color) { create(:option_value, option_type: color, name: 'white') }
+ let!(:black_color) { create(:option_value, option_type: color, name: 'black') }
+ let!(:material) { create(:option_type, name: 'material', presentation: 'Material') }
+ let!(:wool) { create(:option_value, option_type: material, name: 'wool') }
+ let!(:silk) { create(:option_value, option_type: material, name: 'silk') }
+
+ before do
+ material.update_column(:position, 0)
+ silk.update_column(:position, 1)
+ wool.update_column(:position, 2)
+
+ color.update_column(:position, 1)
+ white_color.update_column(:position, 0)
+ black_color.update_column(:position, 10)
+ green_color.update_column(:position, 20)
+
+ size.update_column(:position, 2)
+
+ product_4 = create(:product, option_types: [color, material, size])
+ create(:variant, option_values: [white_color, black_color, wool, silk], product: product_4)
+ end
+
+ it 'orders the option values by option type position and option value position' do
+ positions = available_options.map { |ov| [ov.option_type.position, ov.position] }
+ expect(positions).to eq(positions.sort_by { |e| [e.first, e.second] })
+ end
+ end
end
end
end