Class: Spree::Product
- Inherits:
-
Object
- Object
- Spree::Product
- Includes:
- MemoizedData, Metadata, Metafields, Slugs, Webhooks, ProductScopes, SearchIndexable, StoreScopedResource, TranslatableResource, VendorConcern
- Defined in:
- app/models/spree/product.rb,
app/models/spree/product/slugs.rb,
app/models/spree/product/webhooks.rb
Defined Under Namespace
Constant Summary collapse
- MEMOIZED_METHODS =
%w[total_on_hand taxonomy_ids taxon_and_ancestors default_variant_id tax_category default_variant variant_for_images brand_taxon main_taxon purchasable? in_stock? backorderable? digital?]
- STATUSES =
%w[draft active archived].freeze
- STATUS_TO_WEBHOOK_EVENT =
{ 'active' => 'activated', 'draft' => 'drafted', 'archived' => 'archived' }.freeze
- TRANSLATABLE_FIELDS =
%i[name description slug meta_description meta_title].freeze
Instance Attribute Summary collapse
-
#option_values_hash ⇒ Object
Returns the value of attribute option_values_hash.
-
#prototype_id ⇒ Object
Adding properties and option types on creation based on a chosen prototype.
Class Method Summary collapse
Instance Method Summary collapse
- #any_variant_available?(currency) ⇒ Boolean
- #any_variant_in_stock_or_backorderable? ⇒ Boolean
- #auto_match_taxons ⇒ Object
-
#available? ⇒ Boolean
determine if product is available.
-
#backorderable? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259.
-
#backordered? ⇒ Boolean
determine if any variant (including master) is out of stock and backorderable.
-
#brand ⇒ Spree::Brand, Spree::Taxon
Returns the brand for the product If a brand association is defined (e.g., belongs_to :brand), it will be used Otherwise, falls back to brand_taxon for compatibility.
-
#brand_name ⇒ String
Returns the brand name for the product.
-
#brand_taxon ⇒ Spree::Taxon
Returns the brand taxon for the product.
-
#can_supply? ⇒ Boolean
determine if any variant (including master) can be supplied.
-
#default_image ⇒ Object
deprecated
Deprecated.
Use #primary_media instead.
-
#default_variant ⇒ Spree::Variant
Returns default Variant for Product If ‘track_inventory_levels` is enabled it will try to find the first Variant in stock or backorderable, if there’s none it will return first Variant sorted by ‘position` attribute If `track_inventory_levels` is disabled it will return first Variant sorted by `position` attribute.
-
#default_variant_id ⇒ Integer
Returns default Variant ID for Product.
-
#digital? ⇒ Boolean
Check if the product is digital by checking if any of its shipping methods are digital delivery This is used to determine if the product is digital and should have a digital delivery price instead of a physical shipping price.
- #discontinue! ⇒ Object
- #discontinued? ⇒ Boolean
-
#duplicate ⇒ Object
for adding products which are closely related to existing ones define “duplicate_extra” for site-specific actions, eg for additional fields.
-
#ensure_option_types_exist_for_values_hash ⇒ Object
Ensures option_types and product_option_types exist for keys in option_values_hash.
-
#featured_image ⇒ Object
deprecated
Deprecated.
Use #primary_media instead.
- #find_or_build_master ⇒ Object
-
#find_variant_with_images ⇒ Spree::Variant?
Finds first variant with media using preloaded data when available.
- #first_available_variant(currency) ⇒ Object
- #first_or_default_variant(currency) ⇒ Object
-
#gallery_media ⇒ ActiveRecord::Relation
Returns the product’s media gallery.
-
#has_media? ⇒ Boolean
(also: #has_images?, #has_variant_images?)
Returns true if the product has any media (product-level or variant-level).
-
#has_variants? ⇒ Boolean
Checks if product has variants (non-master variants) Uses variant_count counter cache for performance.
-
#image_count ⇒ Object
deprecated
Deprecated.
Use media_count instead
-
#in_stock? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259.
-
#lowest_price(currency) ⇒ Object
returns the lowest price for the product in the given currency prices_including_master are usually already loaded, so this should not trigger an extra query.
- #main_taxon ⇒ Object
-
#master ⇒ Object
Master variant may be deleted (i.e. when the product is deleted) which would make AR’s default finder return nil.
- #on_sale?(currency) ⇒ Boolean
- #price_varies?(currency) ⇒ Boolean
-
#primary_image ⇒ Object
deprecated
Deprecated.
Use #primary_media instead.
-
#purchasable? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259.
-
#secondary_image ⇒ Spree::Asset?
Returns secondary media for Product (for hover effects).
-
#storefront_description ⇒ String
Returns the short description for the product.
-
#tax_category ⇒ Spree::TaxCategory?
Returns tax category for Product.
- #taxons_for_store(store) ⇒ Object
- #to_csv(store = nil) ⇒ Object
- #to_translation_csv(store = nil, locales = []) ⇒ Object
- #total_on_hand ⇒ Object
-
#update_thumbnail! ⇒ Object
Updates primary_media_id to the first media item.
-
#variant_for_images ⇒ Spree::Variant?
Returns the variant that should be used for displaying images.
-
#variants_and_option_values(current_currency = nil) ⇒ Object
Suitable for displaying only variants that has at least one option value.
Methods included from SearchIndexable
#add_to_search_index, #enqueue_search_index, #enqueue_search_removal, #remove_from_search_index, #search_indexing_enabled?, #search_presentation, #store_ids_for_indexing
Methods included from Slugs
Methods included from Webhooks
#send_product_activated_webhook, #send_product_archived_webhook, #send_product_drafted_webhook
Methods included from Metadata
#metadata, #metadata=, #public_metadata=
Instance Attribute Details
#option_values_hash ⇒ Object
Returns the value of attribute option_values_hash.
182 183 184 |
# File 'app/models/spree/product.rb', line 182 def option_values_hash @option_values_hash end |
#prototype_id ⇒ Object
Adding properties and option types on creation based on a chosen prototype
404 405 406 |
# File 'app/models/spree/product.rb', line 404 def prototype_id @prototype_id end |
Class Method Details
.bulk_auto_match_taxons(store, product_ids) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'app/models/spree/product.rb', line 247 def self.bulk_auto_match_taxons(store, product_ids) return if store.taxons.automatic.none? products_to_auto_match_ids = store.products.not_deleted.not_archived.where(id: product_ids).ids # for ActiveJob 7.1+ if ActiveJob.respond_to?(:perform_all_later) auto_match_taxons_jobs = products_to_auto_match_ids.map do |product_id| Spree::Products::AutoMatchTaxonsJob.new(product_id) end ActiveJob.perform_all_later(auto_match_taxons_jobs) else products_to_auto_match_ids.each { |product_id| Spree::Products::AutoMatchTaxonsJob.perform_later(product_id) } end end |
.like_any(fields, values) ⇒ Object
483 484 485 486 487 488 |
# File 'app/models/spree/product.rb', line 483 def self.like_any(fields, values) conditions = fields.product(values).map do |(field, value)| arel_table[field].matches("%#{value}%") end where conditions.inject(:or) end |
Instance Method Details
#any_variant_available?(currency) ⇒ Boolean
424 425 426 427 428 429 430 |
# File 'app/models/spree/product.rb', line 424 def any_variant_available?(currency) if has_variants? first_available_variant(currency).present? else master.purchasable? && master.price_in(currency).amount.present? end end |
#any_variant_in_stock_or_backorderable? ⇒ Boolean
561 562 563 564 565 566 567 |
# File 'app/models/spree/product.rb', line 561 def any_variant_in_stock_or_backorderable? if has_variants? variants_including_master.in_stock_or_backorderable.exists? else master.in_stock_or_backorderable? end end |
#auto_match_taxons ⇒ Object
578 579 580 581 582 583 584 585 586 |
# File 'app/models/spree/product.rb', line 578 def auto_match_taxons return if deleted? return if archived? store = stores.find_by(default: true) || stores.first return if store.nil? || store.taxons.automatic.none? Spree::Products::AutoMatchTaxonsJob.perform_later(id) end |
#available? ⇒ Boolean
determine if product is available. deleted products and products with status different than active are not available
459 460 461 |
# File 'app/models/spree/product.rb', line 459 def available? active? && !deleted? && (available_on.nil? || available_on <= Time.current) end |
#backorderable? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259
275 276 277 |
# File 'app/models/spree/product.rb', line 275 def backorderable? default_variant.backorderable? || variants.any?(&:backorderable?) end |
#backordered? ⇒ Boolean
determine if any variant (including master) is out of stock and backorderable
479 480 481 |
# File 'app/models/spree/product.rb', line 479 def backordered? variants_including_master.any?(&:backordered?) end |
#brand ⇒ Spree::Brand, Spree::Taxon
Returns the brand for the product If a brand association is defined (e.g., belongs_to :brand), it will be used Otherwise, falls back to brand_taxon for compatibility
518 519 520 521 522 523 524 525 |
# File 'app/models/spree/product.rb', line 518 def brand if self.class.reflect_on_association(:brand) super else Spree::Deprecation.warn('Spree::Product#brand is deprecated and will be removed in Spree 5.5. Please use Spree::Product#brand_taxon instead.') brand_taxon end end |
#brand_name ⇒ String
Returns the brand name for the product
545 546 547 |
# File 'app/models/spree/product.rb', line 545 def brand_name brand&.name end |
#brand_taxon ⇒ Spree::Taxon
Returns the brand taxon for the product
529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'app/models/spree/product.rb', line 529 def brand_taxon @brand_taxon ||= if classification_count.zero? nil elsif Spree.use_translations? taxons.joins(:taxonomy). 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) }) end end |
#can_supply? ⇒ Boolean
determine if any variant (including master) can be supplied
474 475 476 |
# File 'app/models/spree/product.rb', line 474 def can_supply? variants_including_master.any?(&:can_supply?) end |
#default_image ⇒ Object
Use #primary_media instead.
347 348 349 350 |
# File 'app/models/spree/product.rb', line 347 def default_image Spree::Deprecation.warn('Spree::Product#default_image is deprecated and will be removed in Spree 6.0. Please use Spree::Product#primary_media instead.') primary_media end |
#default_variant ⇒ Spree::Variant
Returns default Variant for Product If ‘track_inventory_levels` is enabled it will try to find the first Variant in stock or backorderable, if there’s none it will return first Variant sorted by ‘position` attribute If `track_inventory_levels` is disabled it will return first Variant sorted by `position` attribute
304 305 306 307 308 309 310 |
# File 'app/models/spree/product.rb', line 304 def default_variant @default_variant ||= if Spree::Config[:track_inventory_levels] && has_variants? && available_variant = variants.detect(&:purchasable?) available_variant else has_variants? ? variants.first : find_or_build_master end end |
#default_variant_id ⇒ Integer
Returns default Variant ID for Product
314 315 316 |
# File 'app/models/spree/product.rb', line 314 def default_variant_id @default_variant_id ||= default_variant.id end |
#digital? ⇒ Boolean
Check if the product is digital by checking if any of its shipping methods are digital delivery This is used to determine if the product is digital and should have a digital delivery price instead of a physical shipping price
574 575 576 |
# File 'app/models/spree/product.rb', line 574 def digital? @digital ||= shipping_category&.includes_digital_shipping_method? end |
#discontinue! ⇒ Object
463 464 465 466 467 |
# File 'app/models/spree/product.rb', line 463 def discontinue! self.discontinue_on = Time.current self.status = 'archived' save(validate: false) end |
#discontinued? ⇒ Boolean
469 470 471 |
# File 'app/models/spree/product.rb', line 469 def discontinued? !!discontinue_on && discontinue_on <= Time.current end |
#duplicate ⇒ Object
for adding products which are closely related to existing ones define “duplicate_extra” for site-specific actions, eg for additional fields
452 453 454 |
# File 'app/models/spree/product.rb', line 452 def duplicate Products::Duplicator.call(product: self) end |
#ensure_option_types_exist_for_values_hash ⇒ Object
Ensures option_types and product_option_types exist for keys in option_values_hash
439 440 441 442 443 444 445 446 447 448 |
# File 'app/models/spree/product.rb', line 439 def ensure_option_types_exist_for_values_hash return if option_values_hash.nil? # we need to convert the keys to string to make it work with UUIDs required_option_type_ids = option_values_hash.keys.map(&:to_s) missing_option_type_ids = required_option_type_ids - option_type_ids.map(&:to_s) missing_option_type_ids.each do |id| product_option_types.create(option_type_id: id) end end |
#featured_image ⇒ Object
Use #primary_media instead.
353 354 355 356 |
# File 'app/models/spree/product.rb', line 353 def featured_image Spree::Deprecation.warn('Spree::Product#featured_image is deprecated and will be removed in Spree 6.0. Please use Spree::Product#primary_media instead.') primary_media end |
#find_or_build_master ⇒ Object
283 284 285 |
# File 'app/models/spree/product.rb', line 283 def find_or_build_master master || build_master end |
#find_variant_with_images ⇒ Spree::Variant?
Finds first variant with media using preloaded data when available.
385 386 387 388 389 |
# File 'app/models/spree/product.rb', line 385 def find_variant_with_images return variants.find(&:has_media?) if variants.loaded? variants.joins(:images).first end |
#first_available_variant(currency) ⇒ Object
416 417 418 |
# File 'app/models/spree/product.rb', line 416 def first_available_variant(currency) variants.find { |v| v.purchasable? && v.price_in(currency).amount.present? } end |
#first_or_default_variant(currency) ⇒ Object
406 407 408 409 410 411 412 413 414 |
# File 'app/models/spree/product.rb', line 406 def first_or_default_variant(currency) if !has_variants? default_variant elsif first_available_variant(currency).present? first_available_variant(currency) else variants.first end end |
#gallery_media ⇒ ActiveRecord::Relation
Returns the product’s media gallery. Uses product-level media if present, otherwise falls back to variant images.
321 322 323 324 325 |
# File 'app/models/spree/product.rb', line 321 def gallery_media return media if association(:media).loaded? ? media.any? : media.exists? variant_images end |
#has_media? ⇒ Boolean Also known as: has_images?, has_variant_images?
Returns true if the product has any media (product-level or variant-level). Uses counter cache for performance.
330 331 332 333 334 |
# File 'app/models/spree/product.rb', line 330 def has_media? return variant_images.any? if association(:variant_images).loaded? media_count.positive? end |
#has_variants? ⇒ Boolean
Checks if product has variants (non-master variants) Uses variant_count counter cache for performance
290 291 292 293 294 |
# File 'app/models/spree/product.rb', line 290 def has_variants? return variants.size.positive? if variants.loaded? variant_count.positive? end |
#image_count ⇒ Object
Use media_count instead
371 372 373 |
# File 'app/models/spree/product.rb', line 371 def image_count media_count end |
#in_stock? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259
270 271 272 |
# File 'app/models/spree/product.rb', line 270 def in_stock? @in_stock ||= default_variant.in_stock? || variants.any?(&:in_stock?) end |
#lowest_price(currency) ⇒ Object
returns the lowest price for the product in the given currency prices_including_master are usually already loaded, so this should not trigger an extra query
434 435 436 |
# File 'app/models/spree/product.rb', line 434 def lowest_price(currency) prices_including_master.find_all { |p| p.currency == currency }.min_by(&:amount) end |
#main_taxon ⇒ Object
549 550 551 552 553 |
# File 'app/models/spree/product.rb', line 549 def main_taxon return if classification_count.zero? @main_taxon ||= taxons.first end |
#master ⇒ Object
Master variant may be deleted (i.e. when the product is deleted) which would make AR’s default finder return nil. This is a stopgap for that little problem.
510 511 512 |
# File 'app/models/spree/product.rb', line 510 def master super || variants_including_master.with_deleted.find_by(is_master: true) end |
#on_sale?(currency) ⇒ Boolean
279 280 281 |
# File 'app/models/spree/product.rb', line 279 def on_sale?(currency) prices_including_master.find_all { |p| p.currency == currency }.any?(&:discounted?) end |
#price_varies?(currency) ⇒ Boolean
420 421 422 |
# File 'app/models/spree/product.rb', line 420 def price_varies?(currency) prices_including_master.find_all { |p| p.currency == currency && p.amount.present? }.map(&:amount).uniq.count > 1 end |
#primary_image ⇒ Object
Use #primary_media instead.
359 360 361 362 |
# File 'app/models/spree/product.rb', line 359 def primary_image Spree::Deprecation.warn('Spree::Product#primary_image is deprecated and will be removed in Spree 6.0. Please use Spree::Product#primary_media instead.') primary_media end |
#purchasable? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259
265 266 267 |
# File 'app/models/spree/product.rb', line 265 def purchasable? @purchasable ||= default_variant.purchasable? || variants.any?(&:purchasable?) end |
#secondary_image ⇒ Spree::Asset?
Returns secondary media for Product (for hover effects).
366 367 368 |
# File 'app/models/spree/product.rb', line 366 def secondary_image variant_for_images&.secondary_image end |
#storefront_description ⇒ String
Returns the short description for the product
393 394 395 |
# File 'app/models/spree/product.rb', line 393 def storefront_description description end |
#tax_category ⇒ Spree::TaxCategory?
Returns tax category for Product
399 400 401 |
# File 'app/models/spree/product.rb', line 399 def tax_category @tax_category ||= super || TaxCategory.default end |
#taxons_for_store(store) ⇒ Object
555 556 557 558 559 |
# File 'app/models/spree/product.rb', line 555 def taxons_for_store(store) return if classification_count.zero? taxons.loaded? ? taxons.find_all { |taxon| taxon.taxonomy.store_id == store.id } : taxons.for_store(store) end |
#to_csv(store = nil) ⇒ Object
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 |
# File 'app/models/spree/product.rb', line 588 def to_csv(store = nil) store ||= stores.default || stores.first properties_for_csv = if respond_to?(:product_properties) && Spree::Config.respond_to?(:product_properties_enabled) && 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::MetafieldDefinition.for_resource_type('Spree::Product').order(:namespace, :key).map do |mf_def| .find { |mf| mf. == mf_def.id }&.csv_value end taxons_for_csv ||= taxons.manual.reorder(depth: :desc).first(3).pluck(:pretty_name) taxons_for_csv.fill(nil, taxons_for_csv.size...3) csv_lines = [] all_variants = has_variants? ? variants_including_master.to_a : [master] default_currency = store.default_currency additional_currencies = store.supported_currencies_list.map(&:iso_code) - [default_currency] # Primary rows in the store's default currency all_variants.each_with_index do |variant, index| csv_lines << Spree::CSV::ProductVariantPresenter.new(self, variant, index, properties_for_csv, taxons_for_csv, store, ).call end # Price-only rows for each additional currency additional_currencies.each do |currency| all_variants.each do |variant| next unless variant.amount_in(currency) csv_lines << Spree::CSV::ProductVariantPresenter.new(self, variant, 0, [], [], store, [], currency).call end end csv_lines end |
#to_translation_csv(store = nil, locales = []) ⇒ Object
630 631 632 633 634 635 636 637 638 639 |
# File 'app/models/spree/product.rb', line 630 def to_translation_csv(store = nil, locales = []) locales.filter_map do |locale| # Only export if at least one field has a translation for this locale has_translation = Spree::CSV::ProductTranslationPresenter::TRANSLATABLE_FIELDS.any? do |field| get_field_with_locale(locale, field).present? end Spree::CSV::ProductTranslationPresenter.new(self, locale).call if has_translation end end |
#total_on_hand ⇒ Object
499 500 501 502 503 504 505 |
# File 'app/models/spree/product.rb', line 499 def total_on_hand @total_on_hand ||= if any_variants_not_track_inventory? BigDecimal::INFINITY else stock_items.loaded? ? stock_items.sum(&:count_on_hand) : stock_items.sum(:count_on_hand) end end |
#update_thumbnail! ⇒ Object
Updates primary_media_id to the first media item. Checks product-level media first, then falls back to variant images. Called when media is added, removed, or reordered.
378 379 380 381 |
# File 'app/models/spree/product.rb', line 378 def update_thumbnail! first_media = media.order(:position).first || variant_images.order(:position).first update_column(:primary_media_id, first_media&.id) end |
#variant_for_images ⇒ Spree::Variant?
Returns the variant that should be used for displaying images. Priority: master > default_variant > first variant with images
342 343 344 |
# File 'app/models/spree/product.rb', line 342 def variant_for_images @variant_for_images ||= find_variant_for_images end |
#variants_and_option_values(current_currency = nil) ⇒ Object
Suitable for displaying only variants that has at least one option value. There may be scenarios where an option type is removed and along with it all option values. At that point all variants associated with only those values should not be displayed to frontend users. Otherwise it breaks the idea of having variants
495 496 497 |
# File 'app/models/spree/product.rb', line 495 def variants_and_option_values(current_currency = nil) variants.active(current_currency).joins(:option_value_variants) end |