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
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.rbindex 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 doendendend++ # 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+ endendenddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 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/259def 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/259def 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/259def backorderable?- variants_including_master.any?(&:backorderable?)+ default_variant.backorderable? || variants.any?(&:backorderable?)enddef find_or_build_masterdiff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rbindex 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+ endend- 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+ endendendend