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
Use Product/Variant thumbnail in API
Damian Legawiecd62bbc286b8d
diff --git a/api/app/controllers/spree/api/v3/store/products_controller.rb b/api/app/controllers/spree/api/v3/store/products_controller.rbindex 91d4b3f9f6..8912139d18 100644--- a/api/app/controllers/spree/api/v3/store/products_controller.rb+++ b/api/app/controllers/spree/api/v3/store/products_controller.rb@@ -31,6 +31,7 @@ module Spree# these scopes are not automatically picked by ar_lazy_preload gem and we need to explicitly include themdef scope_includes[+ thumbnail: [attachment_attachment: :blob],master: [:prices, { stock_items: :stock_location }],variants: [:prices, { stock_items: :stock_location }]]diff --git a/api/app/serializers/spree/api/v3/base_serializer.rb b/api/app/serializers/spree/api/v3/base_serializer.rbindex cdcd7affc5..3302449dfb 100644--- a/api/app/serializers/spree/api/v3/base_serializer.rb+++ b/api/app/serializers/spree/api/v3/base_serializer.rb@@ -12,6 +12,7 @@ module Spree# e.g., prod_abc123, var_xyz789, or_def456attribute :id do |object|next nil unless object.respond_to?(:prefix_id)+object.prefix_idenddiff --git a/api/app/serializers/spree/api/v3/product_serializer.rb b/api/app/serializers/spree/api/v3/product_serializer.rbindex c4a1db0ba9..38332b6694 100644--- a/api/app/serializers/spree/api/v3/product_serializer.rb+++ b/api/app/serializers/spree/api/v3/product_serializer.rb@@ -8,6 +8,7 @@ module Spreemeta_description: 'string | null', meta_keywords: 'string | null',variant_count: :number,default_variant_id: :string,+ thumbnail: 'string | null',available_on: 'string | null',purchasable: :boolean, in_stock: :boolean, backorderable: :boolean, available: :boolean,price: 'StorePrice',@@ -39,6 +40,11 @@ module Spreeproduct.default_variant&.prefix_idend+ # Main product image URL for listings (cached thumbnail)+ attribute :thumbnail do |product|+ image_url_for(product.thumbnail)+ end+attribute :tags do |product|product.taggings.map(&:tag)enddiff --git a/api/app/serializers/spree/api/v3/variant_serializer.rb b/api/app/serializers/spree/api/v3/variant_serializer.rbindex e5fadc6beb..e87acd9bb8 100644--- a/api/app/serializers/spree/api/v3/variant_serializer.rb+++ b/api/app/serializers/spree/api/v3/variant_serializer.rb@@ -6,6 +6,7 @@ module Spreeclass VariantSerializer < BaseSerializertypelize product_id: :string, sku: 'string | null',is_master: :boolean, options_text: :string, track_inventory: :boolean, image_count: :number,+ thumbnail: 'string | null',purchasable: :boolean, in_stock: :boolean, backorderable: :boolean,weight: 'number | null', height: 'number | null', width: 'number | null', depth: 'number | null',price: 'StorePrice',@@ -18,6 +19,11 @@ module Spreeattributes :sku, :is_master, :options_text, :track_inventory, :image_count,created_at: :iso8601, updated_at: :iso8601+ # Main variant image URL for listings (cached thumbnail)+ attribute :thumbnail do |variant|+ image_url_for(variant.thumbnail)+ end+attribute :purchasable do |variant|variant.purchasable?enddiff --git a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb b/api/spec/serializers/spree/api/v3/product_serializer_spec.rbindex 08894f7745..be805d4ee1 100644--- a/api/spec/serializers/spree/api/v3/product_serializer_spec.rb+++ b/api/spec/serializers/spree/api/v3/product_serializer_spec.rb@@ -15,6 +15,7 @@ RSpec.describe Spree::Api::V3::ProductSerializer do'slug' => product.slug,'default_variant_id' => product.default_variant.prefix_id)+ expect(subject).to have_key('thumbnail')expect(subject).not_to have_key('sku')expect(subject).not_to have_key('barcode')enddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 84a2369fb0..7e8be938e5 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -20,7 +20,7 @@module Spreeclass Product < Spree.base_class- has_prefix_id :prod # Stripe: prod_+ has_prefix_id :prod # Stripe: prod_acts_as_paranoidacts_as_taggable_on :tags, :labels@@ -613,15 +613,13 @@ module Spreenilelsif Spree.use_translations?taxons.joins(:taxonomy).- join_translation_table(Taxonomy).- find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_brands_name) })- else- if taxons.loaded?- taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_brands_name) }- else- taxons.joins(:taxonomy).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_brands_name) })- end- end+ join_translation_table(Taxonomy).+ find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_brands_name) })+ elsif taxons.loaded?+ taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_brands_name) }+ else+ taxons.joins(:taxonomy).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_brands_name) })+ endend# Returns the brand name for the product@@ -653,12 +651,10 @@ module Spreejoin_translation_table(Taxonomy).order(depth: :desc).find_by(Taxonomy.translation_table_alias => { name: Spree.t(:taxonomy_categories_name) })+ elsif taxons.loaded?+ taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_categories_name) }else- if taxons.loaded?- taxons.find { |taxon| taxon.taxonomy.name == Spree.t(:taxonomy_categories_name) }- else- taxons.joins(:taxonomy).order(depth: :desc).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_categories_name) })- end+ taxons.joins(:taxonomy).order(depth: :desc).find_by(Taxonomy.table_name => { name: Spree.t(:taxonomy_categories_name) })endend@@ -706,15 +702,15 @@ module Spreedef to_csv(store = nil)store ||= stores.default || stores.firstproperties_for_csv = if Spree::Config[:product_properties_enabled]- Spree::Property.order(:position).flat_map do |property|- [- property.name,- product_properties.find { |pp| pp.property_id == property.id }&.value- ]- end- else- []- end+ Spree::Property.order(:position).flat_map do |property|+ [+ property.name,+ product_properties.find { |pp| pp.property_id == property.id }&.value+ ]+ end+ else+ []+ endmetafields_for_csv ||= Spree::MetafieldDefinition.for_resource_type('Spree::Product').order(:namespace, :key).map do |mf_def|metafields.find { |mf| mf.metafield_definition_id == mf_def.id }&.csv_valueenddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex b8a7d64619..5407014e6a 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -94,7 +94,7 @@ module Spreescope :backorderable, -> { left_joins(:stock_items).where(spree_stock_items: { backorderable: true }) }scope :in_stock_or_backorderable, -> { in_stock.or(backorderable) }- scope :eligible, -> {+ scope :eligible, lambda {where(is_master: false).or(where(product_id: Spree::Variant.@@ -105,25 +105,25 @@ module Spree)}- scope :not_discontinued, -> do+ scope :not_discontinued, lambda {where(arel_table[:discontinue_on].eq(nil).or(arel_table[:discontinue_on].gteq(Time.current)))- end+ }scope :not_deleted, -> { where("#{Spree::Variant.quoted_table_name}.deleted_at IS NULL") }- scope :for_currency_and_available_price_amount, ->(currency = nil) do+ scope :for_currency_and_available_price_amount, lambda { |currency = nil|currency ||= Spree::Store.default.default_currencyjoins(:prices).where("#{Spree::Price.table_name}.currency = ?", currency).where("#{Spree::Price.table_name}.amount IS NOT NULL").distinct- end+ }- scope :active, ->(currency = nil) do+ scope :active, lambda { |currency = nil|not_discontinued.not_deleted.for_currency_and_available_price_amount(currency)- end+ }scope :with_option_value, lambda { |option_name, option_value|option_type_ids = OptionType.where(name: option_name).ids@@ -190,7 +190,8 @@ module Spree)self.whitelisted_ransackable_associations = %w[option_values product tax_category prices default_price]- self.whitelisted_ransackable_attributes = %w[weight depth width height sku discontinue_on is_master cost_price cost_currency track_inventory deleted_at]+ self.whitelisted_ransackable_attributes = %w[weight depth width height sku discontinue_on is_master cost_price cost_currency track_inventory+ deleted_at]self.whitelisted_ransackable_scopes = %i(product_name_or_sku_cont search_by_product_name_or_sku)def self.product_name_or_sku_cont(query)@@ -261,9 +262,13 @@ module Spree# @return [String] the options text of the variantdef options_text@options_text ||= if option_values.loaded?- option_values.sort_by { |ov| ov.option_type.position }.map { |ov| "#{ov.option_type.presentation}: #{ov.presentation}" }.to_sentence(words_connector: ', ', two_words_connector: ', ')+ option_values.sort_by do |ov|+ ov.option_type.position+ end.map { |ov| "#{ov.option_type.presentation}: #{ov.presentation}" }.to_sentence(words_connector: ', ', two_words_connector: ', ')else- option_values.includes(:option_type).joins(:option_type).order("#{Spree::OptionType.table_name}.position").map { |ov| "#{ov.option_type.presentation}: #{ov.presentation}" }.to_sentence(words_connector: ', ', two_words_connector: ', ')+ option_values.includes(:option_type).joins(:option_type).order("#{Spree::OptionType.table_name}.position").map do |ov|+ "#{ov.option_type.presentation}: #{ov.presentation}"+ end.to_sentence(words_connector: ', ', two_words_connector: ', ')endenddiff --git a/sdk/src/types/generated/AdminProduct.ts b/sdk/src/types/generated/AdminProduct.tsindex 7ac5b190e7..a82cb226ae 100644--- a/sdk/src/types/generated/AdminProduct.ts+++ b/sdk/src/types/generated/AdminProduct.ts@@ -1,4 +1,4 @@-// Typelizer digest 90ab74b8f6e407e6c3a08e9b58bfd24e+// Typelizer digest a463eb2767d9332308d535dc829e0172//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StorePrice, StoreImage, AdminVariant, StoreOptionType, StoreTaxon} from '@/types'@@ -19,6 +19,7 @@ type AdminProduct = {backorderable: boolean;available: boolean;default_variant_id: string;+ thumbnail: string | null;tags: Array<string>;price: StorePrice;original_price: StorePrice;diff --git a/sdk/src/types/generated/AdminVariant.ts b/sdk/src/types/generated/AdminVariant.tsindex 49e7fdda7c..0ebffcfa6a 100644--- a/sdk/src/types/generated/AdminVariant.ts+++ b/sdk/src/types/generated/AdminVariant.ts@@ -1,4 +1,4 @@-// Typelizer digest f11bb745b5233226e8c325ae5ece6e97+// Typelizer digest cf4d5e568d4b30e287e6592ad80b3d22//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StorePrice, StoreImage, StoreOptionValue, AdminPrice} from '@/types'@@ -13,6 +13,7 @@ type AdminVariant = {image_count: number;created_at: string;updated_at: string;+ thumbnail: string | null;purchasable: boolean;in_stock: boolean;backorderable: boolean;diff --git a/sdk/src/types/generated/StoreProduct.ts b/sdk/src/types/generated/StoreProduct.tsindex 495161420a..6b9c1ac1d3 100644--- a/sdk/src/types/generated/StoreProduct.ts+++ b/sdk/src/types/generated/StoreProduct.ts@@ -1,4 +1,4 @@-// Typelizer digest 3500359bf25409e4d4e7deb376950b65+// Typelizer digest ac3c3a595c61e0ad2e16ef8bec2b46b6//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StorePrice, StoreImage, StoreVariant, StoreOptionType, StoreTaxon} from '@/types'@@ -19,6 +19,7 @@ type StoreProduct = {backorderable: boolean;available: boolean;default_variant_id: string;+ thumbnail: string | null;tags: Array<string>;price: StorePrice;original_price: StorePrice;diff --git a/sdk/src/types/generated/StoreVariant.ts b/sdk/src/types/generated/StoreVariant.tsindex c8e7fc0bae..34664914c0 100644--- a/sdk/src/types/generated/StoreVariant.ts+++ b/sdk/src/types/generated/StoreVariant.ts@@ -1,4 +1,4 @@-// Typelizer digest f725853757a0590127c7335e3eee37e3+// Typelizer digest 8ede60973383d2550f11e82e7c93d873//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {StorePrice, StoreImage, StoreOptionValue} from '@/types'@@ -13,6 +13,7 @@ type StoreVariant = {image_count: number;created_at: string;updated_at: string;+ thumbnail: string | null;purchasable: boolean;in_stock: boolean;backorderable: boolean;