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.

Fixed `Product#has_images?` check (#13475)

Damian Legawiec289ebdd4f221
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 2f089c0c43..f6e1ee8f65 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -250,7 +250,7 @@ module Spree
end
delegate :display_amount, :display_price, :has_default_price?, :track_inventory?,
- :display_compare_at_price, :images, :has_images?, :primary_image, :secondary_image, to: :default_variant
+ :display_compare_at_price, :images, :primary_image, :secondary_image, :image_count, to: :default_variant
alias master_images images
@@ -351,6 +351,10 @@ module Spree
total_image_count.positive?
end
+ # Returns true if the product has any images across all variants.
+ # This is an alias for has_variant_images? for consistency with Variant#has_images?
+ alias has_images? has_variant_images?
+
# Returns default Image for Product
# First it tries to get an image from the master variant
# Fallbacks to the default variant image
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 1bc2127be1..a32befe8c9 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -959,6 +959,69 @@ describe Spree::Product, type: :model do
end
end
+ describe '#has_images?' do
+ let(:product) { create(:product, stores: [store]) }
+
+ context 'when no variants have images' do
+ it 'returns false' do
+ expect(product.has_images?).to be false
+ end
+ end
+
+ context 'when master has images but default_variant (a non-master variant) does not' do
+ let!(:variant) { create(:variant, product: product) }
+ let!(:image) { create(:image, viewable: product.master) }
+
+ it 'returns true because it checks all variants, not just default_variant' do
+ product.reload
+ expect(product.default_variant).to eq(variant)
+ expect(product.default_variant.has_images?).to be false
+ expect(product.has_images?).to be true
+ end
+ end
+
+ context 'when any variant has images' do
+ let!(:variant) { create(:variant, product: product) }
+ let!(:image) { create(:image, viewable: variant) }
+
+ it 'returns true' do
+ product.reload
+ expect(product.has_images?).to be true
+ end
+ end
+ end
+
+ describe '#image_count' do
+ let(:product) { create(:product, stores: [store]) }
+
+ context 'when default_variant is master (no variants)' do
+ it 'returns master image count' do
+ expect(product.image_count).to eq(0)
+ end
+
+ context 'with images on master' do
+ let!(:images) { create_list(:image, 2, viewable: product.master) }
+
+ it 'returns the image count' do
+ product.reload
+ expect(product.image_count).to eq(2)
+ end
+ end
+ end
+
+ context 'when default_variant is a non-master variant' do
+ let!(:variant) { create(:variant, product: product) }
+ let!(:master_images) { create_list(:image, 2, viewable: product.master) }
+ let!(:variant_images) { create_list(:image, 3, viewable: variant) }
+
+ it 'returns the default_variant image count, not master' do
+ product.reload
+ expect(product.default_variant).to eq(variant)
+ expect(product.image_count).to eq(3)
+ end
+ end
+ end
+
describe '#find_first_variant_image' do
let(:product) { create(:product, stores: [store]) }