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.

Support color swatches in Meilisearch filter aggregator (#13868)

Damian Legawiec979a94109d92
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index be5c4a2d92..8ef4d6774f 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -386,7 +386,12 @@ module Spree
ot = ov.option_type
by_option_type[ot] ||= []
- by_option_type[ot] << { id: ov.prefixed_id, name: ov.name, label: ov.label, position: ov.position, count: count }
+ by_option_type[ot] << {
+ id: ov.prefixed_id, name: ov.name, label: ov.label, position: ov.position,
+ color_code: ov.color_code,
+ image_url: ov.image.attached? ? Rails.application.routes.url_helpers.cdn_image_url(ov.image) : nil,
+ count: count
+ }
end
by_option_type.map do |option_type, values|
@@ -395,6 +400,7 @@ module Spree
type: 'option',
name: option_type.name,
label: option_type.label,
+ kind: option_type.kind,
options: values.sort_by { |o| o[:position] }
}
end
diff --git a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
index 73567a60be..1e58e25857 100644
--- a/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
+++ b/spree/core/spec/models/spree/search_provider/meilisearch_spec.rb
@@ -74,6 +74,49 @@ module Spree
expect(ids).to include('price', '-price')
end
+ context 'with option type facets' do
+ let(:color_type) { create(:option_type, :color_swatch, filterable: true) }
+ let(:red_value) { create(:option_value, option_type: color_type, name: 'red', presentation: 'Red', color_code: '#FF0000') }
+ let(:blue_value) { create(:option_value, option_type: color_type, name: 'blue', presentation: 'Blue', color_code: '#0000FF') }
+
+ let(:ms_response) do
+ {
+ 'hits' => [{ 'product_id' => product_1.prefixed_id }],
+ 'estimatedTotalHits' => 1,
+ 'facetDistribution' => {
+ 'option_value_ids' => {
+ red_value.prefixed_id => 3,
+ blue_value.prefixed_id => 1
+ }
+ }
+ }
+ end
+
+ it 'includes kind on option type filters' do
+ result = provider.search_and_filter(scope: store.products, query: '')
+ color_filter = result.filters.find { |f| f[:name] == 'color' }
+
+ expect(color_filter).to be_present
+ expect(color_filter[:kind]).to eq('color_swatch')
+ end
+
+ it 'includes color_code on option values' do
+ result = provider.search_and_filter(scope: store.products, query: '')
+ color_filter = result.filters.find { |f| f[:name] == 'color' }
+ red_option = color_filter[:options].find { |o| o[:name] == 'red' }
+
+ expect(red_option[:color_code]).to eq('#FF0000')
+ end
+
+ it 'includes image_url as nil when no image attached' do
+ result = provider.search_and_filter(scope: store.products, query: '')
+ color_filter = result.filters.find { |f| f[:name] == 'color' }
+ red_option = color_filter[:options].find { |o| o[:name] == 'red' }
+
+ expect(red_option[:image_url]).to be_nil
+ end
+ end
+
context 'with no results' do
let(:ms_response) { { 'hits' => [], 'estimatedTotalHits' => 0, 'facetDistribution' => {} } }