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
[NX-7] Filter taxons by taxonomies' permalinks (#11288)
Piotr Milewskief2343a5555a
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yamlindex 731d84d1fc..c3c21fdfce 100644--- a/api/docs/v2/storefront/index.yaml+++ b/api/docs/v2/storefront/index.yaml@@ -3599,6 +3599,12 @@ paths:type: stringexample: '1'description: Fetch children nodes of specified Taxon+ - in: query+ name: 'filter[parent_permalink]'+ schema:+ type: string+ example: 'clothes'+ description: Fetch children nodes of specified Taxon permalink- in: queryname: 'filter[taxonomy_id]'schema:diff --git a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rbindex d13cc5bd28..95057a273a 100644--- a/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/taxons_spec.rb@@ -65,6 +65,21 @@ describe 'Taxons Spec', type: :request doendend+ context 'by parent permalink' do+ let!(:taxonomy_3) { create(:taxonomy, store: taxonomy.store) }+ let!(:taxon_3) { create(:taxon, taxonomy: taxonomy_3, parent: taxonomy_3.root) }++ before { get "/api/v2/storefront/taxons?filter[parent_permalink]=#{taxonomy.root.permalink}" }++ it_behaves_like 'returns 200 HTTP status'++ it 'returns children taxons by parent' do+ expect(json_response['data'].size).to eq(2)+ expect(json_response['data'][0]).to have_relationship(:parent).with_data('id' => taxonomy.root.id.to_s, 'type' => 'taxon')+ expect(json_response['data'][1]).to have_relationship(:parent).with_data('id' => taxonomy.root.id.to_s, 'type' => 'taxon')+ end+ end+context 'by taxonomy' dobefore { get "/api/v2/storefront/taxons?taxonomy_id=#{taxonomy.id}" }diff --git a/core/app/finders/spree/taxons/find.rb b/core/app/finders/spree/taxons/find.rbindex f1813c3a2c..1aa4563b89 100644--- a/core/app/finders/spree/taxons/find.rb+++ b/core/app/finders/spree/taxons/find.rb@@ -3,16 +3,18 @@ module Spreeclass Finddef initialize(scope:, params:)@scope = scope- @ids = String(params.dig(:filter, :ids)).split(',')- @parent = params.dig(:filter, :parent_id)- @taxonomy = params.dig(:filter, :taxonomy_id)- @name = params.dig(:filter, :name)- @roots = params.dig(:filter, :roots)+ @ids = String(params.dig(:filter, :ids)).split(',')+ @parent = params.dig(:filter, :parent_id)+ @parent_permalink = params.dig(:filter, :parent_permalink)+ @taxonomy = params.dig(:filter, :taxonomy_id)+ @name = params.dig(:filter, :name)+ @roots = params.dig(:filter, :roots)enddef executetaxons = by_ids(scope)taxons = by_parent(taxons)+ taxons = by_parent_permalink(taxons)taxons = by_taxonomy(taxons)taxons = by_roots(taxons)taxons = by_name(taxons)@@ -22,7 +24,7 @@ module Spreeprivate- attr_reader :ids, :parent, :taxonomy, :roots, :name, :scope+ attr_reader :ids, :parent, :parent_permalink, :taxonomy, :roots, :name, :scopedef ids?ids.present?@@ -32,6 +34,10 @@ module Spreeparent.present?end+ def parent_permalink?+ parent_permalink.present?+ end+def taxonomy?taxonomy.present?end@@ -60,6 +66,16 @@ module Spreetaxons.where(parent_id: parent)end+ def by_parent_permalink(taxons)+ return taxons unless parent_permalink?++ if Rails::VERSION::STRING >= '6.1'+ taxons.joins(:parent).where(parent: { permalink: parent_permalink })+ else+ taxons.joins("INNER JOIN #{Spree::Taxon.table_name} AS parent_taxon ON parent_taxon.id = #{Spree::Taxon.table_name}.parent_id").where(["parent_taxon.permalink = ?", parent_permalink])+ end+ end+def by_taxonomy(taxons)return taxons unless taxonomy?