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
Fix `ascend_by_price` and `descend_by_price` scopes (#13749)
Damian Legawiecc19b75a91659
diff --git a/spree/api/app/controllers/spree/api/v3/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/resource_controller.rbindex 07d2ac0846..efb3dede41 100644--- a/spree/api/app/controllers/spree/api/v3/resource_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/resource_controller.rb@@ -108,7 +108,14 @@ module Spreeransack(ransack_params)result = @search.result(distinct: collection_distinct?)result = apply_collection_sort(result)- @pagy, @collection = pagy(result, limit: limit, page: page)+ pagy_options = { limit: limit, page: page }+ # When custom scopes add select values (e.g. price sorting), pre-compute+ # the count without those selects to avoid Mobility's select_for_count+ # crashing on non-Arel select values.+ if result.select_values.any?+ pagy_options[:count] = result.except(:select, :order).count(:all)+ end+ @pagy, @collection = pagy(result, **pagy_options)@collectionenddiff --git a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rbindex b52a200dab..6b9e0d7171 100644--- a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb@@ -177,6 +177,22 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller doexpect(prices).to eq(prices.sort.reverse)end+ it 'paginates correctly when sorting by price' do+ get :index, params: { sort: 'price', page: 1, limit: 1 }++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].size).to eq(1)+ expect(json_response['meta']['pages']).to be >= 2+ end++ it 'paginates correctly when sorting by price descending' do+ get :index, params: { sort: '-price', page: 1, limit: 1 }++ expect(response).to have_http_status(:ok)+ expect(json_response['data'].size).to eq(1)+ expect(json_response['meta']['pages']).to be >= 2+ end+it 'sorts by best selling' doSpree::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)diff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rbindex a3105ae813..a000010fb7 100644--- a/spree/core/app/models/concerns/spree/product_scopes.rb+++ b/spree/core/app/models/concerns/spree/product_scopes.rb@@ -43,15 +43,22 @@ module Spree# Price sorting scopes that use a derived table JOIN to get prices across all variants.# These ensure products with only variant prices (no master price) are included in results.+ #+ # Uses Arel::Nodes::As for select expressions so that:+ # - PG allows ORDER BY with DISTINCT (expressions must appear in SELECT list)+ # - Mobility's select_for_count can safely call .right on all select_valuesadd_search_scope :ascend_by_price doprice_agg_sql = Price.non_zero.joins(:variant).select("#{Variant.table_name}.product_id AS product_id, MIN(#{Price.table_name}.amount) AS agg_price").group("#{Variant.table_name}.product_id").to_sql+ price_expr = Arel.sql('COALESCE(price_agg.agg_price, 999999999)')+joins("LEFT JOIN (#{price_agg_sql}) AS price_agg ON price_agg.product_id = #{Product.table_name}.id").- select("#{Product.table_name}.*", 'COALESCE(price_agg.agg_price, 999999999) AS min_price').- order(Arel.sql('COALESCE(price_agg.agg_price, 999999999) ASC'))+ select("#{Product.table_name}.*").+ select(Arel::Nodes::As.new(price_expr, Arel.sql('min_price'))).+ order(price_expr.asc)endadd_search_scope :descend_by_price do@@ -60,9 +67,12 @@ module Spree.group("#{Variant.table_name}.product_id").to_sql+ price_expr = Arel.sql('COALESCE(price_agg.agg_price, 0)')+joins("LEFT JOIN (#{price_agg_sql}) AS price_agg ON price_agg.product_id = #{Product.table_name}.id").- select("#{Product.table_name}.*", 'COALESCE(price_agg.agg_price, 0) AS max_price').- order(Arel.sql('COALESCE(price_agg.agg_price, 0) DESC'))+ select("#{Product.table_name}.*").+ select(Arel::Nodes::As.new(price_expr, Arel.sql('max_price'))).+ order(price_expr.desc)endadd_search_scope :price_between do |low, high|