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
Fixed: cleanup and standarize in_stock/out_of_scope scopes for Produc… (#13698)
Damian Legawiec83fe4741d850
diff --git a/spree/admin/app/helpers/spree/admin/products_helper.rb b/spree/admin/app/helpers/spree/admin/products_helper.rbindex 8a9f0e6f1a..552cf78ee6 100644--- a/spree/admin/app/helpers/spree/admin/products_helper.rb+++ b/spree/admin/app/helpers/spree/admin/products_helper.rb@@ -90,9 +90,9 @@ module Spreeenddef product_filter_stock_dropdown_value- if params.dig(:q, :in_stock_items) == '1'+ if params.dig(:q, :in_stock) == '1'Spree.t('admin.products.in_stock')- elsif params.dig(:q, :out_of_stock_items) == '1'+ elsif params.dig(:q, :out_of_stock) == '1'Spree.t('admin.products.out_of_stock')elseSpree.t('admin.products.any_stock')diff --git a/spree/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erb b/spree/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erbindex 2a0d0e1283..09172a7279 100644--- a/spree/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erb+++ b/spree/admin/app/views/spree/admin/products/_table_filter_dropdown.html.erb@@ -16,7 +16,7 @@<%= dropdown_menu do %><h6 class="dropdown-header"><%= Spree.t('admin.products.by_status') %>:</h6>- <%= active_link_to Spree.t('admin.products.all_statuses'), params.to_unsafe_h.deep_merge({page: nil, q: {status_eq: nil}}), class: 'dropdown-item', active: (params[:q] || {}).values_at(*%w[status_eq in_stock_items out_of_stock_items] ).all?(&:blank?) %>+ <%= active_link_to Spree.t('admin.products.all_statuses'), params.to_unsafe_h.deep_merge({page: nil, q: {status_eq: nil}}), class: 'dropdown-item', active: (params[:q] || {}).values_at(*%w[status_eq in_stock out_of_stock] ).all?(&:blank?) %><%= link_to Spree.t('admin.products.active'),params.to_unsafe_h.deep_merge({page: nil, q: {status_eq: :active}}),class: "dropdown-item #{'active' if params[:q][:status_eq] == 'active'}" %>@@ -31,12 +31,12 @@<h6 class="dropdown-header"><%= Spree.t('admin.products.by_stock') %>:</h6>- <%= active_link_to Spree.t('admin.products.any_stock'), params.to_unsafe_h.deep_merge({page: nil, q: {in_stock_items: nil, out_of_stock_items: nil}}), class: 'dropdown-item', active: (params[:q] || {}).values_at(*%w[status_eq in_stock_items out_of_stock_items] ).all?(&:blank?) %>+ <%= active_link_to Spree.t('admin.products.any_stock'), params.to_unsafe_h.deep_merge({page: nil, q: {in_stock: nil, out_of_stock: nil}}), class: 'dropdown-item', active: (params[:q] || {}).values_at(*%w[status_eq in_stock out_of_stock] ).all?(&:blank?) %><%= link_to Spree.t('admin.products.in_stock'),- params.to_unsafe_h.deep_merge({page: nil, q: {in_stock_items: '1', out_of_stock_items: nil}}),- class: "dropdown-item #{'active' if params[:q][:in_stock_items] == '1'}" %>+ params.to_unsafe_h.deep_merge({page: nil, q: {in_stock: '1', out_of_stock: nil}}),+ class: "dropdown-item #{'active' if params[:q][:in_stock] == '1'}" %><%= link_to Spree.t('admin.products.out_of_stock'),- params.to_unsafe_h.deep_merge({page: nil, q: {out_of_stock_items: '1', in_stock_items: nil}}),- class: "dropdown-item #{'active' if params[:q][:out_of_stock_items] == '1'}" %>+ params.to_unsafe_h.deep_merge({page: nil, q: {out_of_stock: '1', in_stock: nil}}),+ class: "dropdown-item #{'active' if params[:q][:out_of_stock] == '1'}" %><% end %><% end %>diff --git a/spree/admin/config/initializers/spree_admin_tables.rb b/spree/admin/config/initializers/spree_admin_tables.rbindex b96858d9db..2e01dc3c0a 100644--- a/spree/admin/config/initializers/spree_admin_tables.rb+++ b/spree/admin/config/initializers/spree_admin_tables.rb@@ -98,7 +98,7 @@ Rails.application.config.after_initialize dodisplayable: false,default: false,position: 70,- ransack_attribute: 'in_stock_items'+ ransack_attribute: 'in_stock'# Taxons - displayed as comma-separated list, filtered via autocompleteSpree.admin.tables.products.add :taxons,diff --git a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rbindex 593a2f4713..381fe72c7d 100644--- a/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb+++ b/spree/admin/spec/controllers/spree/admin/products_controller_spec.rb@@ -1483,12 +1483,12 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller doendit 'returns only in stock products' do- get :index, params: { q: { in_stock_items: 1 } }+ get :index, params: { q: { in_stock: 1 } }expect(assigns(:products).to_a).to eq([in_stock_product])endit 'returns only out of stock products' do- get :index, params: { q: { out_of_stock_items: 1 } }+ get :index, params: { q: { out_of_stock: 1 } }expect(assigns(:products).to_a).to eq([out_of_stock_product])endenddiff --git a/spree/core/app/finders/spree/products/find.rb b/spree/core/app/finders/spree/products/find.rbindex 87b7cc1b41..2791b45178 100644--- a/spree/core/app/finders/spree/products/find.rb+++ b/spree/core/app/finders/spree/products/find.rb@@ -22,14 +22,8 @@ module Spree@in_stock = params.dig(:filter, :in_stock)@backorderable = params.dig(:filter, :backorderable)@purchasable = params.dig(:filter, :purchasable)- @out_of_stock = params.dig(:filter, :out_of_stock).to_b@tags = params.dig(:filter, :tags).to_s.split(',').compact_blank@vendor_ids = params.dig(:filter, :vendor_ids)&.split(',')&.compact_blank || []-- if @purchasable.present? && @out_of_stock.present?- @purchasable = false- @out_of_stock = false- endenddef execute@@ -50,7 +44,6 @@ module Spreeproducts = show_only_stock(products)products = show_only_backorderable(products)products = show_only_purchasable(products)- products = show_only_out_of_stock(products)products = by_taxonomies(products)products = ordered(products)products = by_vendor_ids(products)@@ -62,7 +55,7 @@ module Spreeattr_reader :ids, :skus, :price, :currency, :taxons, :concat_taxons, :name, :options, :option_value_ids, :scope,:sort_by, :deleted, :discontinued, :store, :in_stock, :backorderable, :purchasable, :tags,- :query, :vendor_ids, :out_of_stock, :slug, :taxonomies+ :query, :vendor_ids, :slug, :taxonomiesdef query?query.present?@@ -294,12 +287,6 @@ module Spreeproducts.in_stock_or_backorderableend- def show_only_out_of_stock(products)- return products unless out_of_stock.present?-- products.out_of_stock- end-def map_prices(prices)prices.map do |price|price == 'Infinity' ? BigDecimal::INFINITY : price.to_fdiff --git a/spree/core/app/models/concerns/spree/product_scopes.rb b/spree/core/app/models/concerns/spree/product_scopes.rbindex c2f4b340c6..b6c6116db9 100644--- a/spree/core/app/models/concerns/spree/product_scopes.rb+++ b/spree/core/app/models/concerns/spree/product_scopes.rb@@ -81,9 +81,26 @@ module Spreewhere(Price.table_name => { amount: price.. })end- add_search_scope :in_stock do- joins(:variants_including_master).merge(Spree::Variant.in_stock)+ # Can't use add_search_scope for this as it needs a default argument+ # Ransack calls with '1' to activate, '0' or nil to skip+ # In Ruby code: in_stock(true) for in-stock, in_stock(false) for out-of-stock+ def self.in_stock(in_stock = true)+ if in_stock == '0' || !in_stock+ all+ else+ joins(:variants_including_master).merge(Spree::Variant.in_stock_or_backorderable)+ end+ end+ search_scopes << :in_stock++ def self.out_of_stock(out_of_stock = true)+ if out_of_stock == '0' || !out_of_stock+ all+ else+ where.not(id: joins(:variants_including_master).merge(Spree::Variant.in_stock_or_backorderable))+ endend+ search_scopes << :out_of_stockadd_search_scope :backorderable dojoins(:variants_including_master).merge(Spree::Variant.backorderable)diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex 182425a4ae..0a126b80b7 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -197,18 +197,6 @@ module Spreescope :by_source, ->(source) { send(source) }scope :paused, -> { where(status: 'paused') }scope :published, -> { where(status: 'active') }- scope :in_stock_items, -> { joins(:variants).merge(Spree::Variant.in_stock_or_backorderable) }- scope :out_of_stock_items, lambda {- joins(variants_including_master: :stock_items).- where(spree_variants: { track_inventory: true }).- where.not(id: Spree::Variant.where(track_inventory: false).pluck(:product_id).uniq).- where(spree_stock_items: { backorderable: false }).- group(:id).- having("SUM(#{Spree::StockItem.table_name}.count_on_hand) <= 0")- }- scope :out_of_stock, lambda {- joins(:stock_items).where("#{Spree::Variant.table_name}.track_inventory = ? OR #{Spree::StockItem.table_name}.count_on_hand <= ?", false, 0)- }attr_accessor :option_values_hash@@ -232,7 +220,7 @@ module Spreeself.whitelisted_ransackable_associations = %w[taxons stores variants_including_master master variants tags labelsshipping_category classifications option_types]self.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name in_taxon price_between- multi_search in_stock_items out_of_stock_items with_option_value_ids+ multi_search in_stock out_of_stock with_option_value_idsascend_by_price descend_by_price][diff --git a/spree/core/spec/finders/spree/products/find_spec.rb b/spree/core/spec/finders/spree/products/find_spec.rbindex e53e63b679..681b8569df 100644--- a/spree/core/spec/finders/spree/products/find_spec.rb+++ b/spree/core/spec/finders/spree/products/find_spec.rb@@ -67,7 +67,7 @@ module Spreeendcontext 'in stock' do- it 'returns products with variants in stock' do+ it 'returns products with variants in stock or backorderable' doparams = {filter: {ids: '',@@ -89,7 +89,7 @@ module Spreescope: Spree::Product.all,params: params).execute- ).to contain_exactly(in_stock_product, not_backorderable_product)+ ).to contain_exactly(product, product_2, product_3, in_stock_product, not_backorderable_product)endenddiff --git a/spree/core/spec/models/spree/product/scopes_spec.rb b/spree/core/spec/models/spree/product/scopes_spec.rbindex be42a7648e..fa397786c9 100644--- a/spree/core/spec/models/spree/product/scopes_spec.rb+++ b/spree/core/spec/models/spree/product/scopes_spec.rb@@ -215,10 +215,66 @@ describe 'Product scopes', type: :model doendend- # Regression test for SD-1439 ambiguous column name: count_on_hand- describe '#in_stock.in_stock_or_backorderable' do- it do- expect { Spree::Product.in_stock.in_stock_or_backorderable.count }.not_to raise_error+ describe '#in_stock / #out_of_stock' do+ let!(:in_stock_product) do+ create(:product, stores: [store]).tap do |p|+ p.stock_items.update_all(count_on_hand: 10, backorderable: false)+ end+ end++ let!(:backorderable_product) do+ create(:product, stores: [store]).tap do |p|+ p.stock_items.update_all(count_on_hand: 0, backorderable: true)+ end+ end++ let!(:out_of_stock_product) do+ create(:product, stores: [store]).tap do |p|+ p.stock_items.update_all(count_on_hand: 0, backorderable: false)+ end+ end++ describe '#in_stock' do+ it 'returns products with stock on hand' do+ expect(Spree::Product.in_stock).to include(in_stock_product)+ end++ it 'returns backorderable products' do+ expect(Spree::Product.in_stock).to include(backorderable_product)+ end++ it 'excludes products with no stock and not backorderable' do+ expect(Spree::Product.in_stock).not_to include(out_of_stock_product)+ end++ it 'returns all products when called with false (no filtering)' do+ result = Spree::Product.in_stock(false)+ expect(result).to include(in_stock_product, backorderable_product, out_of_stock_product)+ end++ it 'returns all products when called with "0" (Ransack skip)' do+ result = Spree::Product.in_stock('0')+ expect(result).to include(in_stock_product, backorderable_product, out_of_stock_product)+ end++ # Regression test for SD-1439 ambiguous column name: count_on_hand+ it 'can be chained with other scopes without error' do+ expect { Spree::Product.in_stock.in_stock_or_backorderable.count }.not_to raise_error+ end+ end++ describe '#out_of_stock' do+ it 'returns products with no stock and not backorderable' do+ expect(Spree::Product.out_of_stock).to include(out_of_stock_product)+ end++ it 'excludes in-stock products' do+ expect(Spree::Product.out_of_stock).not_to include(in_stock_product)+ end++ it 'excludes backorderable products' do+ expect(Spree::Product.out_of_stock).not_to include(backorderable_product)+ endendend