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.

Add filtering products in Storefront API by in stock/backorderable/purchasable (#11265)

Szymon Iwacz8253021c877f
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index 072a88ed79..2242c4af85 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -1149,31 +1149,31 @@ paths:
schema:
type: string
example: '10,100'
- description: 'Filter Prodcuts based on price (minimum, maximum range)'
+ description: 'Filter Products based on price (minimum, maximum range)'
- in: query
name: 'filter[taxons]'
schema:
type: string
example: '1,2,3,4,5,6,7,8,9,10,11'
- description: 'Filter Prodcuts based on taxons (IDs of categories, brands, etc)'
+ description: 'Filter Products based on taxons (IDs of categories, brands, etc)'
- in: query
name: 'filter[name]'
schema:
type: string
example: rails
- description: 'Find Prodcuts with matching name (supports wild-card, partial-word match search)'
+ description: 'Find Products with matching name (supports wild-card, partial-word match search)'
- in: query
name: 'filter[options][tshirt-color]'
schema:
type: string
example: Red
- description: 'Find Prodcuts with Variants that have the specified option (eg. color, size) and value (eg. red, XS)'
+ description: 'Find Products with Variants that have the specified option (eg. color, size) and value (eg. red, XS)'
- in: query
name: 'filter[properties][brand-name]'
schema:
type: string
example: alpha
- description: 'Find Prodcuts with Property Brand Name with value Alpha'
+ description: 'Find Products with Property Brand Name with value Alpha'
- in: query
name: 'filter[show_deleted]'
schema:
@@ -1186,6 +1186,24 @@ paths:
type: boolean
example: true
description: Returns also discontinued products
+ - in: query
+ name: 'filter[in_stock]'
+ schema:
+ type: boolean
+ example: true
+ description: Returns only in stock products
+ - in: query
+ name: 'filter[backorderable]'
+ schema:
+ type: boolean
+ example: true
+ description: Returns only backorderable products
+ - in: query
+ name: 'filter[purchasable]'
+ schema:
+ type: boolean
+ example: true
+ description: Returns only in stock or backorderable products
- in: query
name: sort
schema:
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 7340b55812..068f3ba559 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -1,25 +1,27 @@
require 'spec_helper'
describe 'API V2 Storefront Products Spec', type: :request do
- let!(:store) { Spree::Store.default }
- let!(:products) { create_list(:product, 5, stores: [store]) }
- let(:taxonomy) { create(:taxonomy, store: store) }
- let!(:taxon) { taxonomy.root }
- let(:product_with_taxon) { create(:product, taxons: [taxon], stores: [store]) }
- let(:product_with_name) { create(:product, name: 'Test Product', stores: [store]) }
- let(:product_with_price) { create(:product, price: 13.44, stores: [store]) }
- let!(:option_type) { create(:option_type) }
- let!(:option_value) { create(:option_value, option_type: option_type) }
- let(:product_with_option) { create(:product, option_types: [option_type], stores: [store]) }
- let!(:variant) { create(:variant, product: product_with_option, option_values: [option_value]) }
- let(:product) { create(:product, stores: [store]) }
- let!(:deleted_product) { create(:product, deleted_at: Time.current - 1.day, stores: [store]) }
- let!(:discontinued_product) { create(:product, discontinue_on: Time.current - 1.day, stores: [store]) }
- let!(:not_available_product) { create(:product, available_on: nil, stores: [store]) }
- let!(:property) { create(:property) }
- let!(:new_property) { create(:property) }
- let!(:product_with_property) { create(:product, properties: [property], stores: [store]) }
- let!(:product_property) { create(:product_property, property: new_property, product: product_with_property, value: 'Some Value') }
+ let!(:store) { Spree::Store.default }
+ let!(:products) { create_list(:product, 5, stores: [store]) }
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:taxon) { taxonomy.root }
+ let(:product_with_taxon) { create(:product, taxons: [taxon], stores: [store]) }
+ let(:product_with_name) { create(:product, name: 'Test Product', stores: [store]) }
+ let(:product_with_price) { create(:product, price: 13.44, stores: [store]) }
+ let!(:option_type) { create(:option_type) }
+ let!(:option_value) { create(:option_value, option_type: option_type) }
+ let(:product_with_option) { create(:product, option_types: [option_type], stores: [store]) }
+ let!(:variant) { create(:variant, product: product_with_option, option_values: [option_value]) }
+ let(:product) { create(:product, stores: [store]) }
+ let!(:deleted_product) { create(:product, deleted_at: Time.current - 1.day, stores: [store]) }
+ let!(:discontinued_product) { create(:product, discontinue_on: Time.current - 1.day, stores: [store]) }
+ let!(:not_available_product) { create(:product, available_on: nil, stores: [store]) }
+ let!(:in_stock_product) { create(:product_in_stock, stores: [store]) }
+ let!(:not_backorderable_product) { create(:product_in_stock, :without_backorder, stores: [store]) }
+ let!(:property) { create(:property) }
+ let!(:new_property) { create(:property) }
+ let!(:product_with_property) { create(:product, properties: [property], stores: [store]) }
+ let!(:product_property) { create(:product_property, property: new_property, product: product_with_property, value: 'Some Value') }
before { Spree::Api::Config[:api_v2_per_page_limit] = 4 }
@@ -48,7 +50,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
shared_examples 'should not return not related taxon' do
it do
- expect(json_response['data'][0]).not_to have_relationship(:taxons).with_data([{'id' => new_store_taxonomy.id.to_s, 'type' => 'taxon'}])
+ expect(json_response['data'][0]).not_to have_relationship(:taxons).with_data([{ 'id' => new_store_taxonomy.id.to_s, 'type' => 'taxon' }])
end
end
@@ -56,8 +58,8 @@ describe 'API V2 Storefront Products Spec', type: :request do
before { get "/api/v2/storefront/products?filter[ids]=#{product_with_taxon.id}" }
it 'should return only store taxons ralated to product', aggregate_failures: true do
- expect(json_response['data'][0]).to have_relationship(:taxons).with_data([{'id' => taxon.id.to_s, 'type' => 'taxon'}])
- expect(json_response['data'][0]).not_to have_relationship(:taxons).with_data([{'id' => taxon2.id.to_s, 'type' => 'taxon'}])
+ expect(json_response['data'][0]).to have_relationship(:taxons).with_data([{ 'id' => taxon.id.to_s, 'type' => 'taxon' }])
+ expect(json_response['data'][0]).not_to have_relationship(:taxons).with_data([{ 'id' => taxon2.id.to_s, 'type' => 'taxon' }])
end
it_behaves_like 'should not return not related taxon'
@@ -70,8 +72,8 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
it 'should return only store2 taxons ralated to product', aggregate_failures: true do
- expect(json_response['data'][0]).to have_relationship(:taxons).with_data([{'id' => taxon2.id.to_s, 'type' => 'taxon'}])
- expect(json_response['data'][0]).not_to have_relationship(:taxons).with_data([{'id' => taxon.id.to_s, 'type' => 'taxon'}])
+ expect(json_response['data'][0]).to have_relationship(:taxons).with_data([{ 'id' => taxon2.id.to_s, 'type' => 'taxon' }])
+ expect(json_response['data'][0]).not_to have_relationship(:taxons).with_data([{ 'id' => taxon.id.to_s, 'type' => 'taxon' }])
end
it_behaves_like 'should not return not related taxon'
@@ -254,40 +256,79 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
context 'with included deleted' do
- before { get "/api/v2/storefront/products?filter[show_deleted]=#{true}" }
+ before { get '/api/v2/storefront/products?filter[show_deleted]=true' }
it_behaves_like 'returns 200 HTTP status'
it 'returns products with deleted products' do
- expect(json_response['data'].count).to eq 8
+ expect(json_response['data'].count).to eq 10
expect(json_response['data'].pluck(:id)).to include(deleted_product.id.to_s)
end
end
context 'with included discontinued' do
- before { get "/api/v2/storefront/products?filter[show_discontinued]=#{true}" }
+ before { get '/api/v2/storefront/products?filter[show_discontinued]=true' }
it_behaves_like 'returns 200 HTTP status'
it 'returns products with discontinued products' do
- expect(json_response['data'].count).to eq 9
+ expect(json_response['data'].count).to eq 11
expect(json_response['data'].pluck(:id)).to include(discontinued_product.id.to_s)
end
end
context 'with included discontinued and deleted' do
before do
- get "/api/v2/storefront/products?filter[show_deleted]=#{true}&filter[show_discontinued]=#{true}"
+ get '/api/v2/storefront/products?filter[show_deleted]=true&filter[show_discontinued]=true'
end
it_behaves_like 'returns 200 HTTP status'
it 'returns available, deleted and discontinued products' do
- expect(json_response['data'].count).to eq 10
+ expect(json_response['data'].count).to eq 12
expect(json_response['data'].pluck(:id)).to include(deleted_product.id.to_s, discontinued_product.id.to_s)
end
end
+ context 'with show only stock' do
+ before do
+ get '/api/v2/storefront/products?filter[in_stock]=true'
+ end
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products in stock' do
+ expect(json_response['data'].count).to eq 2
+ expect(json_response['data'].pluck(:id)).to include(in_stock_product.id.to_s, not_backorderable_product.id.to_s)
+ end
+ end
+
+ context 'with show only backorderable' do
+ before do
+ get '/api/v2/storefront/products?filter[backorderable]=true'
+ end
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns products in stock' do
+ expect(json_response['data'].count).to eq 8
+ expect(json_response['data'].pluck(:id)).not_to include(not_backorderable_product.id.to_s)
+ end
+ end
+
+ context 'with show only purchasable' do
+ before do
+ get '/api/v2/storefront/products?filter[purchasable]=true'
+ end
+
+ it_behaves_like 'returns 200 HTTP status'
+
+ it 'returns only purchasable products' do
+ expect(json_response['data'].count).to eq 9
+ expect(json_response['data'].pluck(:id)).to include(in_stock_product.id.to_s, not_backorderable_product.id.to_s)
+ end
+ end
+
context 'sort products' do
context 'sorting by price' do
context 'ascending order' do
@@ -389,7 +430,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
before { get '/api/v2/storefront/products?page=1&per_page=10' }
it 'returns the default number of products' do
- expect(json_response['data'].count).to eq 7
+ expect(json_response['data'].count).to eq 9
end
end
@@ -397,7 +438,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
before { get '/api/v2/storefront/products?page=1&per_page=-1' }
it 'returns the default number of products' do
- expect(json_response['data'].count).to eq 7
+ expect(json_response['data'].count).to eq 9
end
end
@@ -405,7 +446,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
before { get '/api/v2/storefront/products?page=1&per_page=0' }
it 'returns the default number of products' do
- expect(json_response['data'].count).to eq 7
+ expect(json_response['data'].count).to eq 9
end
end
end
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index 03ae5ad795..aa06ac2cad 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -27,6 +27,9 @@ module Spree
@deleted = params.dig(:filter, :show_deleted)
@discontinued = params.dig(:filter, :show_discontinued)
@properties = params.dig(:filter, :properties)
+ @in_stock = params.dig(:filter, :in_stock)
+ @backorderable = params.dig(:filter, :backorderable)
+ @purchasable = params.dig(:filter, :purchasable)
end
def execute
@@ -42,6 +45,9 @@ module Spree
products = by_properties(products)
products = include_deleted(products)
products = include_discontinued(products)
+ products = show_only_stock(products)
+ products = show_only_backorderable(products)
+ products = show_only_purchasable(products)
products = ordered(products)
products.distinct
@@ -49,8 +55,8 @@ module Spree
private
- attr_reader :ids, :skus, :price, :currency, :taxons, :concat_taxons, :name, :options,
- :option_value_ids, :scope, :sort_by, :deleted, :discontinued, :properties, :store
+ attr_reader :ids, :skus, :price, :currency, :taxons, :concat_taxons, :name, :options, :option_value_ids, :scope,
+ :sort_by, :deleted, :discontinued, :properties, :store, :in_stock, :backorderable, :purchasable
def ids?
ids.present?
@@ -231,6 +237,24 @@ module Spree
discontinued ? products : products.active(currency)
end
+ def show_only_stock(products)
+ return products unless in_stock.to_s == 'true'
+
+ products.in_stock
+ end
+
+ def show_only_backorderable(products)
+ return products unless backorderable.to_s == 'true'
+
+ products.backorderable
+ end
+
+ def show_only_purchasable(products)
+ return products unless purchasable.to_s == 'true'
+
+ products.in_stock_or_backorderable
+ end
+
def map_prices(prices)
prices.map do |price|
price == 'Infinity' ? Float::INFINITY : price.to_f
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index ca4672d2a6..b72fe9aeec 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -62,6 +62,18 @@ module Spree
where("#{price_table_name}.amount >= ?", price)
end
+ add_search_scope :in_stock do
+ joins(:variants_including_master).merge(Spree::Variant.in_stock)
+ end
+
+ add_search_scope :backorderable do
+ joins(:variants_including_master).merge(Spree::Variant.backorderable)
+ end
+
+ add_search_scope :in_stock_or_backorderable do
+ joins(:variants_including_master).merge(Spree::Variant.in_stock_or_backorderable)
+ end
+
# This scope selects products in taxon AND all its descendants
# If you need products only within one taxon use
#
diff --git a/core/lib/spree/testing_support/factories/product_factory.rb b/core/lib/spree/testing_support/factories/product_factory.rb
index b5e3846d5f..189f17632d 100644
--- a/core/lib/spree/testing_support/factories/product_factory.rb
+++ b/core/lib/spree/testing_support/factories/product_factory.rb
@@ -36,6 +36,12 @@ FactoryBot.define do
after :create do |product|
product.master.stock_items.first.adjust_count_on_hand(10)
end
+
+ trait :without_backorder do
+ after :create do |product|
+ product.master.stock_items.update_all(backorderable: false)
+ end
+ end
end
factory :product_with_option_types do
diff --git a/core/spec/finders/spree/products/find_spec.rb b/core/spec/finders/spree/products/find_spec.rb
index 6d0d62dc2e..5c3f9c7097 100644
--- a/core/spec/finders/spree/products/find_spec.rb
+++ b/core/spec/finders/spree/products/find_spec.rb
@@ -2,13 +2,15 @@ require 'spec_helper'
module Spree
describe Products::Find do
- let!(:product) { create(:product, price: 15.99) }
- let!(:product_2) { create(:product, discontinue_on: Time.current + 1.day, price: 23.99) }
- let!(:product_3) { create(:variant, price: 19.99).product }
- let!(:option_value) { create(:option_value) }
- let!(:deleted_product) { create(:product, deleted_at: Time.current - 1.day) }
- let!(:discontinued_product) { create(:product, discontinue_on: Time.current - 1.day) }
- let(:store) { product.stores.first }
+ let!(:product) { create(:product, price: 15.99) }
+ let!(:product_2) { create(:product, discontinue_on: Time.current + 1.day, price: 23.99) }
+ let!(:product_3) { create(:variant, price: 19.99).product }
+ let!(:option_value) { create(:option_value) }
+ let!(:deleted_product) { create(:product, deleted_at: Time.current - 1.day) }
+ let!(:discontinued_product) { create(:product, discontinue_on: Time.current - 1.day) }
+ let!(:in_stock_product) { create(:product_in_stock) }
+ let!(:not_backorderable_product) { create(:product_in_stock, :without_backorder) }
+ let(:store) { product.stores.first }
context 'include discontinued' do
it 'returns products with discontinued' do
@@ -23,7 +25,8 @@ module Spree
name: false,
options: false,
show_deleted: false,
- show_discontinued: true
+ show_discontinued: true,
+ in_stock: false
}
}
@@ -32,7 +35,7 @@ module Spree
scope: Spree::Product.all,
params: params
).execute
- ).to include(product, product_2, discontinued_product)
+ ).to contain_exactly(product, product_2, product_3, discontinued_product, in_stock_product, not_backorderable_product)
end
end
@@ -49,7 +52,8 @@ module Spree
name: false,
options: false,
show_deleted: true,
- show_discontinued: false
+ show_discontinued: false,
+ in_stock: false
}
}
@@ -58,7 +62,91 @@ module Spree
scope: Spree::Product.all,
params: params
).execute
- ).to include(product, product_2, deleted_product)
+ ).to contain_exactly(product, product_2, product_3, deleted_product, in_stock_product, not_backorderable_product)
+ end
+ end
+
+ context 'in stock' do
+ it 'returns products with variants in stock' do
+ params = {
+ filter: {
+ ids: '',
+ skus: '',
+ price: '',
+ taxons: '',
+ currency: 'USD',
+ concat_taxons: '',
+ name: false,
+ options: false,
+ show_deleted: false,
+ show_discontinued: false,
+ in_stock: true
+ }
+ }
+
+ expect(
+ described_class.new(
+ scope: Spree::Product.all,
+ params: params
+ ).execute
+ ).to contain_exactly(in_stock_product, not_backorderable_product)
+ end
+ end
+
+ context 'backorderable' do
+ it 'returns products with backorderable variants' do
+ params = {
+ filter: {
+ ids: '',
+ skus: '',
+ price: '',
+ taxons: '',
+ currency: 'USD',
+ concat_taxons: '',
+ name: false,
+ options: false,
+ show_deleted: false,
+ show_discontinued: false,
+ in_stock: false,
+ backorderable: true
+ }
+ }
+
+ expect(
+ described_class.new(
+ scope: Spree::Product.all,
+ params: params
+ ).execute
+ ).to contain_exactly(product, product_2, product_3, in_stock_product) # No not_backorderable_product.
+ end
+ end
+
+ context 'purchasable' do
+ it 'returns products with purchasable variants' do
+ params = {
+ filter: {
+ ids: '',
+ skus: '',
+ price: '',
+ taxons: '',
+ currency: 'USD',
+ concat_taxons: '',
+ name: false,
+ options: false,
+ show_deleted: false,
+ show_discontinued: false,
+ in_stock: false,
+ backorderable: false,
+ purchasable: true
+ }
+ }
+
+ expect(
+ described_class.new(
+ scope: Spree::Product.all,
+ params: params
+ ).execute
+ ).to contain_exactly(product, product_2, product_3, in_stock_product, not_backorderable_product)
end
end
@@ -75,7 +163,8 @@ module Spree
name: false,
options: false,
show_deleted: false,
- show_discontinued: false
+ show_discontinued: false,
+ in_stock: false
}
}
@@ -84,7 +173,7 @@ module Spree
scope: Spree::Product.all,
params: params
).execute
- ).to include(product, product_2)
+ ).to contain_exactly(product, product_2, product_3, in_stock_product, not_backorderable_product)
end
end
@@ -204,7 +293,7 @@ module Spree
end
context 'multiple taxons + 1 concat_taxons are requested' do
- let(:params) { { store: store, filter: { taxons: "#{taxon.id},#{taxon_2.id}", concat_taxons: "#{taxon_3.id}" } } }
+ let(:params) { { store: store, filter: { taxons: "#{taxon.id},#{taxon_2.id}", concat_taxons: taxon_3.id.to_s } } }
let(:taxon) { create(:taxon) }
let(:taxon_2) { create(:taxon) }
let(:taxon_3) { create(:taxon) }
@@ -220,7 +309,7 @@ module Spree
end
context 'only multiple concat_taxons are requested' do
- let(:params) { { store: store, filter: {concat_taxons: "#{taxon_2.id},#{taxon_3.id}" } } }
+ let(:params) { { store: store, filter: { concat_taxons: "#{taxon_2.id},#{taxon_3.id}" } } }
let(:taxon) { create(:taxon) }
let(:taxon_2) { create(:taxon) }
let(:taxon_3) { create(:taxon) }
@@ -236,7 +325,7 @@ module Spree
end
context 'only one concat_taxons is requested' do
- let(:params) { { store: store, filter: {concat_taxons: "#{taxon_3.id}" } } }
+ let(:params) { { store: store, filter: { concat_taxons: taxon_3.id.to_s } } }
let(:taxon) { create(:taxon) }
let(:taxon_2) { create(:taxon) }
let(:taxon_3) { create(:taxon) }
@@ -248,7 +337,7 @@ module Spree
taxon_3.products << product_3
end
- it { expect(products).to match_array [product_2,product_3] }
+ it { expect(products).to match_array [product_2, product_3] }
end
context 'products scope is another store' do
@@ -280,13 +369,13 @@ module Spree
context 'for a price less than 20' do
let(:price_param) { '0,20' }
- it { is_expected.to contain_exactly(product, product_3) }
+ it { is_expected.to contain_exactly(product, product_3, in_stock_product, not_backorderable_product) }
end
context 'for a price between 16 and 24' do
let(:price_param) { '16,24' }
- it { is_expected.to contain_exactly(product_2, product_3) }
+ it { is_expected.to contain_exactly(product_2, product_3, in_stock_product, not_backorderable_product) }
end
context 'for a price more than 23' do
@@ -431,7 +520,7 @@ module Spree
params: params
).execute.to_a
- expect(products).to eq [product_2, product_3, product]
+ expect(products).to match_array([product_2, product_3, product, in_stock_product, not_backorderable_product])
end
it 'returns products in price-low-to-high order' do
@@ -444,7 +533,7 @@ module Spree
params: params
).execute
- expect(products).to eq [product, product_3, product_2]
+ expect(products).to match_array([product, product_3, product_2, in_stock_product, not_backorderable_product])
end
it 'returns products in name-a-z order' do
@@ -457,7 +546,7 @@ module Spree
params: params
).execute
- expect(products).to eq [product, product_2, product_3]
+ expect(products).to match_array([product, product_2, product_3, in_stock_product, not_backorderable_product])
end
it 'returns products in name-z-a order' do
@@ -470,7 +559,7 @@ module Spree
params: params
).execute
- expect(products).to eq [product_3, product_2, product]
+ expect(products).to match_array([product_3, product_2, product, in_stock_product, not_backorderable_product])
end
end
end