Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: product finder filter[taxons] by permalink #10059

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/docs/v2/storefront/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,8 @@ paths:
name: filter[taxons]
schema:
type: string
example: 1,2,3,4,5,6,7,8,9,10,11
description: Filter Prodcuts based on taxons (IDs of categories, brands, etc)
example: 1,2,3,4,5,men,men/tshirts,women,women/tshirts
description: Filter Prodcuts based on taxons (IDs or Permalinks of categories, brands, etc)
- in: query
name: filter[name]
schema:
Expand Down
12 changes: 11 additions & 1 deletion core/app/finders/spree/products/find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,17 @@ def by_price(products)
def by_taxons(products)
return products unless taxons?

products.joins(:taxons).where(spree_taxons: { id: taxons })
ids = taxons.select { |t| t =~ /^\d+$/ }
permalinks = taxons + ids - (taxons & ids)

c = Spree::Taxon.connection

clause = ''
clause += Spree::Taxon.sanitize_sql_for_conditions(["#{c.quote_table_name('spree_taxons')}.#{c.quote_column_name('id')} IN (?)", ids]) if ids.any?
aforty marked this conversation as resolved.
Show resolved Hide resolved
clause += ' OR ' if ids.any? && permalinks.any?
clause += Spree::Taxon.sanitize_sql_for_conditions(["#{c.quote_table_name('spree_taxons')}.#{c.quote_column_name('permalink')} IN (?)", permalinks]) if permalinks.any?

products.joins(:taxons).where(clause)
end

def by_name(products)
Expand Down
57 changes: 57 additions & 0 deletions core/spec/finders/spree/products/find_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,63 @@ module Spree
end
end

describe 'filter by taxons' do
subject(:result) do
described_class.new(
scope: Spree::Product.all,
params: params,
current_currency: 'USD'
).execute
end

let!(:taxonomy) { create :taxonomy, name: 'Root' }

let!(:taxon_1) { create :taxon, taxonomy: taxonomy, name: 'Parent' }
let!(:taxon_2) { create :taxon, taxonomy: taxonomy, parent: taxon_1, name: 'Child 1' }
let!(:taxon_3) { create :taxon, taxonomy: taxonomy, name: 'Parent 2' }

let(:product_1) { create :product, taxons: [taxon_1] }
let(:product_2) { create :product, taxons: [taxon_1, taxon_2] }
let(:product_3) { create :product, taxons: [taxon_3] }

let!(:variant_1) { create :variant, product: product_1 }
let!(:variant_2) { create :variant, product: product_2 }
let!(:variant_3) { create :variant, product: product_3 }

let(:filter_taxons) { [] }
let(:params) do
{
filter: {
taxons: filter_taxons
}
}
end

context 'filtering by taxon ID\'s' do
let(:filter_taxons) { "#{taxon_1.id},#{taxon_3.id}" }

it 'returns products that match ID\'s' do
expect(result).to match_array([product_1, product_2, product_3])
end
end

context 'filtering by taxon permalinks' do
let(:filter_taxons) { "#{taxon_2.permalink}" }

it 'returns products that match permalinks' do
expect(result).to match_array([product_2])
end
end

context 'filtering by taxon ID\'s and permalinks mixed' do
let(:filter_taxons) { "#{taxon_2.id},#{taxon_3.permalink}" }

it 'returns products that match ID\'s and permalinks' do
expect(result).to match_array([product_2, product_3])
end
end
end

context 'ordered' do
it 'returns products in default order' do
params = {
Expand Down