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.

Fixed: Storefront Products API now works with old slugs as well (#12066)

Damian Legawiec9749b104cef3
diff --git a/api/app/controllers/spree/api/v2/storefront/products_controller.rb b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
index 6b33a14089..90b1d5fb52 100644
--- a/api/app/controllers/spree/api/v2/storefront/products_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/products_controller.rb
@@ -16,7 +16,14 @@ module Spree
end
def resource
- @resource ||= find_with_fallback_default_locale { scope.find_by(slug: params[:id]) } || scope.find(params[:id])
+ # fixed N+1 queries issue
+ variant_includes = [
+ :prices,
+ { option_values: [:option_type] }
+ ]
+
+ # using FriendlyId so old slugs still work
+ @resource ||= find_with_fallback_default_locale { scope.includes(variants: variant_includes, master: variant_includes).friendly.find(params[:id]) } || scope.includes(variants: variant_includes, master: variant_includes).friendly.find(params[:id])
end
def collection_sorter
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
index ba6b44e3e6..d5b91a1845 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -1171,6 +1171,21 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
+ context 'with old slug' do
+ before do
+ old_slug = product.slug
+ product.update(slug: 'new-slug')
+ expect(product.reload.slug).to eq 'new-slug'
+ get "/api/v2/storefront/products/#{old_slug}"
+ end
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'still finds the product' do
+ expect(json_response['data']['id']).to eq(product.id.to_s)
+ end
+ end
+
context 'with product image data' do
shared_examples 'returns product image data' do
it 'returns product image data' do