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.

SD-1462 Product #purchasable?, #in_stock?, #backorderable? will use default_variant (#11329)

Konrad Gorazd7b0eda52a947
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 ed18c3eb0b..1263c259ff 100644
--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb
@@ -822,5 +822,47 @@ describe 'API V2 Storefront Products Spec', type: :request do
end
end
end
+
+ # Regression test for SD-1462 - shouldn't use master variant if there are any non master variants
+ context 'with variants' do
+ let(:variant) { create(:variant, product: product) }
+ let(:request) { get "/api/v2/storefront/products/#{product.slug}" }
+
+ describe 'purchasable attribute value' do
+ before do
+ product.master.stock_items.update_all(backorderable: true)
+ variant.stock_items.update_all(backorderable: false)
+ request
+ end
+
+ it 'uses variants purchasability only' do
+ expect(json_response['data']['attributes']['backorderable']).to eq(false)
+ end
+ end
+
+ describe 'backorderable attribute value' do
+ before do
+ product.master.stock_items.update_all(backorderable: true)
+ variant.stock_items.update_all(backorderable: false)
+ request
+ end
+
+ it 'uses variants backorderability only' do
+ expect(json_response['data']['attributes']['backorderable']).to eq(false)
+ end
+ end
+
+ describe 'in_stock attribute value' do
+ before do
+ product.master.stock_items.update_all(count_on_hand: 10)
+ variant.stock_items.update_all(count_on_hand: 0)
+ request
+ end
+
+ it 'uses variants #in_stock? only' do
+ expect(json_response['data']['attributes']['in_stock']).to eq(false)
+ end
+ end
+ end
end
end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index fd4df7c08f..7919e1e343 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -142,17 +142,17 @@ module Spree
# Cant use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259
def purchasable?
- variants_including_master.any?(&:purchasable?)
+ default_variant.purchasable? || variants.any?(&:purchasable?)
end
# Cant use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259
def in_stock?
- variants_including_master.any?(&:in_stock?)
+ default_variant.in_stock? || variants.any?(&:in_stock?)
end
# Cant use short form block syntax due to https://github.com/Netflix/fast_jsonapi/issues/259
def backorderable?
- variants_including_master.any?(&:backorderable?)
+ default_variant.backorderable? || variants.any?(&:backorderable?)
end
def find_or_build_master
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index a00b792fea..6f8fe6acda 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -16,18 +16,39 @@ describe Spree::Product, type: :model do
%w[purchasable backorderable in_stock].each do |method_name|
describe "#{method_name}?" do
- before { allow(product).to receive(:variants_including_master) { [product.master, variant] } }
+ context 'with variants' do
+ it "returns false if no variant is #{method_name.humanize.downcase} even if master is" do
+ variant.stock_items.update_all(backorderable: false) if %w[purchasable backorderable].include?(method_name)
+ variant.stock_items.update_all(count_on_hand: 0) if method_name == 'in_stock'
- it "returns false if no variant is #{method_name.humanize.downcase}" do
- allow_any_instance_of(Spree::Variant).to receive("#{method_name}?").and_return(false)
+ product.master.stock_items.where(variant: product.master).update_all(backorderable: true) if %w[purchasable backorderable].include?(method_name)
+ product.master.stock_items.where(variant: product.master).update_all(count_on_hand: 10) if method_name == 'in_stock'
- expect(product.send("#{method_name}?")).to eq false
+ expect(product.send("#{method_name}?")).to eq false
+ end
+
+ it "returns true if variant is #{method_name.humanize.downcase}" do
+ variant.stock_items.update_all(backorderable: true) if %w[purchasable backorderable].include?(method_name)
+ variant.stock_items.update_all(count_on_hand: 10) if method_name == 'in_stock'
+
+ expect(product.send("#{method_name}?")).to eq true
+ end
end
- it "returns true if variant that is #{method_name.humanize.downcase} exists" do
- allow(variant).to receive("#{method_name}?").and_return(true)
+ context 'without variants' do
+ it "returns false if master is not #{method_name.humanize.downcase}" do
+ product.master.stock_items.update_all(backorderable: false) if %w[purchasable backorderable].include?(method_name)
+ product.master.stock_items.update_all(count_on_hand: 0) if method_name == 'in_stock'
- expect(product.send("#{method_name}?")).to eq true
+ expect(product.send("#{method_name}?")).to eq false
+ end
+
+ it "returns true if master is #{method_name.humanize.downcase}" do
+ product.master.stock_items.update_all(backorderable: true) if %w[purchasable backorderable].include?(method_name)
+ product.master.stock_items.update_all(count_on_hand: 10) if method_name == 'in_stock'
+
+ expect(product.send("#{method_name}?")).to eq true
+ end
end
end
end