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 best selling sort in Products API (#13632)

Damian Legawieca204e815022d
diff --git a/backend/engines/api/app/controllers/concerns/spree/api/v3/http_caching.rb b/backend/engines/api/app/controllers/concerns/spree/api/v3/http_caching.rb
index 723f2e6289..5da5847428 100644
--- a/backend/engines/api/app/controllers/concerns/spree/api/v3/http_caching.rb
+++ b/backend/engines/api/app/controllers/concerns/spree/api/v3/http_caching.rb
@@ -70,9 +70,10 @@ module Spree
# Build a cache key for a collection
# Includes: query params, pagination, includes, currency, locale
+ # Strips order to avoid PostgreSQL errors with DISTINCT + subquery ORDER BY expressions
def collection_cache_key(collection)
parts = [
- collection.cache_key_with_version,
+ collection.reorder(nil).cache_key_with_version,
params[:include],
params[:q],
params[:page],
diff --git a/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb b/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb
index c73a7f4926..da8f9b23c8 100644
--- a/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/backend/engines/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -5,8 +5,7 @@ module Spree
class ProductsController < ResourceController
SORT_OPTIONS = {
'price-low-to-high' => :ascend_by_price,
- 'price-high-to-low' => :descend_by_price,
- 'best-selling' => :by_best_selling
+ 'price-high-to-low' => :descend_by_price
}.freeze
protected
@@ -54,6 +53,8 @@ module Spree
sort_by = params.dig(:q, :sort_by) || params[:sort_by]
return collection unless sort_by.present?
+ return collection.distinct(false).reorder(nil).by_best_selling if sort_by == 'best-selling'
+
scope_method = SORT_OPTIONS[sort_by]
return collection.reorder(nil).send(scope_method) if scope_method.present?
diff --git a/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index 9725b6525a..f16366dd39 100644
--- a/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/backend/engines/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -177,6 +177,17 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(prices).to eq(prices.sort.reverse)
end
+ it 'sorts by best selling' do
+ Spree::StoreProduct.find_by(product: product, store: store).update!(units_sold_count: 10, revenue: 100)
+ Spree::StoreProduct.find_by(product: product2, store: store).update!(units_sold_count: 50, revenue: 500)
+
+ get :index, params: { sort_by: 'best-selling' }
+
+ expect(response).to have_http_status(:ok)
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids.first).to eq(product2.prefixed_id)
+ end
+
it 'sorts by name a-z with ransack' do
get :index, params: { q: { s: 'name asc' } }
diff --git a/backend/engines/core/app/models/concerns/spree/product_scopes.rb b/backend/engines/core/app/models/concerns/spree/product_scopes.rb
index d8d9010185..108a12c21e 100644
--- a/backend/engines/core/app/models/concerns/spree/product_scopes.rb
+++ b/backend/engines/core/app/models/concerns/spree/product_scopes.rb
@@ -350,16 +350,17 @@ module Spree
# @param order_direction [Symbol] :desc (default) or :asc
# @return [ActiveRecord::Relation]
add_search_scope :by_best_selling do |order_direction = :desc|
- order_dir = order_direction == :desc ? 'DESC' : 'ASC'
store_id = Spree::Current.store&.id
+ sp_table = StoreProduct.arel_table
+ products_table = Product.arel_table
- joins(:store_products)
- .where(StoreProduct.table_name => { store_id: store_id })
- .select("#{Product.table_name}.*",
- "#{StoreProduct.table_name}.units_sold_count",
- "#{StoreProduct.table_name}.revenue")
- .order(Arel.sql("#{StoreProduct.table_name}.units_sold_count #{order_dir}"))
- .order(Arel.sql("#{StoreProduct.table_name}.revenue #{order_dir}"))
+ conditions = sp_table[:product_id].eq(products_table[:id]).and(sp_table[:store_id].eq(store_id))
+
+ units_sold = Arel::Nodes::NamedFunction.new('COALESCE', [sp_table.project(sp_table[:units_sold_count]).where(conditions), 0])
+ revenue = Arel::Nodes::NamedFunction.new('COALESCE', [sp_table.project(sp_table[:revenue]).where(conditions), 0])
+
+ order_dir = order_direction == :desc ? :desc : :asc
+ order(units_sold.send(order_dir)).order(revenue.send(order_dir))
end
# .search_by_name