Class: Spree::Product

Inherits:
Object
  • Object
show all
Includes:
MemoizedData, Metadata, MultiStoreResource, Slugs, Webhooks, ProductScopes, TranslatableResource, VendorConcern
Defined in:
app/models/spree/product.rb,
app/models/spree/product/slugs.rb,
app/models/spree/product/webhooks.rb

Defined Under Namespace

Modules: Slugs, Webhooks

Constant Summary collapse

MEMOIZED_METHODS =
%w[total_on_hand taxonomy_ids taxon_and_ancestors category
default_variant_id tax_category default_variant
default_image secondary_image
purchasable? in_stock? backorderable? has_variants? digital?]
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Slugs

#ensure_slug_is_unique

Methods included from Webhooks

#send_product_activated_webhook, #send_product_archived_webhook, #send_product_drafted_webhook

Instance Attribute Details

#option_values_hashObject

Returns the value of attribute option_values_hash.



215
216
217
# File 'app/models/spree/product.rb', line 215

def option_values_hash
  @option_values_hash
end

#prototype_idObject

Adding properties and option types on creation based on a chosen prototype



386
387
388
# File 'app/models/spree/product.rb', line 386

def prototype_id
  @prototype_id
end

Class Method Details

.bulk_auto_match_taxons(store, product_ids) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/models/spree/product.rb', line 279

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



480
481
482
483
484
485
# File 'app/models/spree/product.rb', line 480

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

Returns:

  • (Boolean)


406
407
408
409
410
411
412
# File 'app/models/spree/product.rb', line 406

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

Returns:

  • (Boolean)


592
593
594
595
596
597
598
# File 'app/models/spree/product.rb', line 592

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_taxonsObject



609
610
611
612
613
614
615
616
617
# File 'app/models/spree/product.rb', line 609

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

Returns:

  • (Boolean)


448
449
450
# File 'app/models/spree/product.rb', line 448

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

Returns:

  • (Boolean)


307
308
309
# File 'app/models/spree/product.rb', line 307

def backorderable?
  default_variant.backorderable? || variants.any?(&:backorderable?)
end

#backordered?Boolean

determine if any variant (including master) is out of stock and backorderable

Returns:

  • (Boolean)


468
469
470
# File 'app/models/spree/product.rb', line 468

def backordered?
  variants_including_master.any?(&:backordered?)
end

#brandObject



553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'app/models/spree/product.rb', line 553

def brand
  @brand ||= if 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
end

#can_supply?Boolean

determine if any variant (including master) can be supplied

Returns:

  • (Boolean)


463
464
465
# File 'app/models/spree/product.rb', line 463

def can_supply?
  variants_including_master.any?(&:can_supply?)
end

#categorise_variants_from_option(opt_type) ⇒ Object

split variants list into hash which shows mapping of opt value onto matching variants eg categorise_variants_from_option(color) => -> […], “blue” -> […]



474
475
476
477
478
# File 'app/models/spree/product.rb', line 474

def categorise_variants_from_option(opt_type)
  return {} unless option_types.include?(opt_type)

  variants.active.group_by { |v| v.option_values.detect { |o| o.option_type == opt_type } }
end

#categoryObject



567
568
569
570
571
572
573
574
575
576
577
578
579
580
# File 'app/models/spree/product.rb', line 567

def category
  @category ||= if Spree.use_translations?
                  taxons.joins(:taxonomy).
                    join_translation_table(Taxonomy).
                    order(depth: :desc).
                    find_by(Taxonomy.translation_table_alias => { 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
                end
end

#default_imageSpree::Image Also known as: featured_image

Returns default Image for Product

Returns:



348
349
350
351
352
353
354
355
356
# File 'app/models/spree/product.rb', line 348

def default_image
  @default_image ||= if images.any?
                       images.first
                     elsif default_variant.images.any?
                       default_variant.default_image
                     elsif variant_images.any?
                       variant_images.first
                     end
end

#default_variantSpree::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

Returns:



332
333
334
335
336
337
338
# File 'app/models/spree/product.rb', line 332

def default_variant
  @default_variant ||= if Spree::Config[:track_inventory_levels] && available_variant = variants.detect(&:purchasable?)
                         available_variant
                       else
                         has_variants? ? variants.first : find_or_build_master
                       end
end

#default_variant_idInteger

Returns default Variant ID for Product

Returns:

  • (Integer)


342
343
344
# File 'app/models/spree/product.rb', line 342

def default_variant_id
  @default_variant_id ||= default_variant.id
end

#deleted?Boolean

use deleted? rather than checking the attribute directly. this allows extensions to override deleted? if they want to provide their own definition.

Returns:

  • (Boolean)


441
442
443
# File 'app/models/spree/product.rb', line 441

def deleted?
  !!deleted_at
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

Returns:

  • (Boolean)


605
606
607
# File 'app/models/spree/product.rb', line 605

def digital?
  @digital ||= shipping_methods&.digital&.exists?
end

#discontinue!Object



452
453
454
455
456
# File 'app/models/spree/product.rb', line 452

def discontinue!
  self.discontinue_on = Time.current
  self.status = 'archived'
  save(validate: false)
end

#discontinued?Boolean

Returns:

  • (Boolean)


458
459
460
# File 'app/models/spree/product.rb', line 458

def discontinued?
  !!discontinue_on && discontinue_on <= Time.current
end

#duplicateObject

for adding products which are closely related to existing ones define “duplicate_extra” for site-specific actions, eg for additional fields



434
435
436
# File 'app/models/spree/product.rb', line 434

def duplicate
  Products::Duplicator.call(product: self)
end

#empty_option_values?Boolean

Returns:

  • (Boolean)


496
497
498
499
500
# File 'app/models/spree/product.rb', line 496

def empty_option_values?
  options.empty? || options.any? do |opt|
    opt.option_type.option_values.empty?
  end
end

#ensure_option_types_exist_for_values_hashObject

Ensures option_types and product_option_types exist for keys in option_values_hash



421
422
423
424
425
426
427
428
429
430
# File 'app/models/spree/product.rb', line 421

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

#find_or_build_masterObject



315
316
317
# File 'app/models/spree/product.rb', line 315

def find_or_build_master
  master || build_master
end

#first_available_variant(currency) ⇒ Object



398
399
400
# File 'app/models/spree/product.rb', line 398

def first_available_variant(currency)
  variants.find { |v| v.purchasable? && v.price_in(currency).amount.present? }
end

#first_or_default_variant(currency) ⇒ Object



388
389
390
391
392
393
394
395
396
# File 'app/models/spree/product.rb', line 388

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

#has_variants?Boolean

the master variant is not a member of the variants array

Returns:

  • (Boolean)


320
321
322
# File 'app/models/spree/product.rb', line 320

def has_variants?
  @has_variants ||= variants.loaded? ? variants.size.positive? : variants.any?
end

#in_stock?Boolean

Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259

Returns:

  • (Boolean)


302
303
304
# File 'app/models/spree/product.rb', line 302

def in_stock?
  @in_stock ||= default_variant.in_stock? || variants.in_stock.any?
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



416
417
418
# File 'app/models/spree/product.rb', line 416

def lowest_price(currency)
  prices_including_master.find_all { |p| p.currency == currency }.min_by(&:amount)
end

#main_taxonObject



582
583
584
# File 'app/models/spree/product.rb', line 582

def main_taxon
  category || taxons.first
end

#masterObject

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.



549
550
551
# File 'app/models/spree/product.rb', line 549

def master
  super || variants_including_master.with_deleted.find_by(is_master: true)
end

#on_sale?(currency) ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
# File 'app/models/spree/product.rb', line 311

def on_sale?(currency)
  prices_including_master.find_all { |p| p.currency == currency }.any?(&:discounted?)
end

#page_builder_urlObject



643
644
645
646
647
# File 'app/models/spree/product.rb', line 643

def page_builder_url
  return unless Spree::Core::Engine.routes.url_helpers.respond_to?(:product_path)

  Spree::Core::Engine.routes.url_helpers.product_path(self)
end

#price_varies?(currency) ⇒ Boolean

Returns:

  • (Boolean)


402
403
404
# File 'app/models/spree/product.rb', line 402

def price_varies?(currency)
  prices_including_master.find_all { |p| p.currency == currency && p.amount.present? }.map(&:amount).uniq.count > 1
end

#property(property_name) ⇒ Object



502
503
504
505
506
507
508
# File 'app/models/spree/product.rb', line 502

def property(property_name)
  if product_properties.loaded?
    product_properties.detect { |property| property.property.name == property_name }.try(:value)
  else
    product_properties.joins(:property).find_by(spree_properties: { name: property_name }).try(:value)
  end
end

#purchasable?Boolean

Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259

Returns:

  • (Boolean)


297
298
299
# File 'app/models/spree/product.rb', line 297

def purchasable?
  @purchasable ||= default_variant.purchasable? || variants.in_stock_or_backorderable.any?
end

#remove_property(property_name) ⇒ Object



534
535
536
# File 'app/models/spree/product.rb', line 534

def remove_property(property_name)
  product_properties.joins(:property).find_by(spree_properties: { name: property_name.parameterize })&.destroy
end

#secondary_imageSpree::Image

Returns secondary Image for Product

Returns:



361
362
363
364
365
366
367
368
369
370
371
# File 'app/models/spree/product.rb', line 361

def secondary_image
  @secondary_image ||= if images.size > 1
                         images.second
                       elsif images.size == 1 && default_variant.images.size.positive?
                         default_variant.images.first
                       elsif default_variant.images.size > 1
                         default_variant.secondary_image
                       elsif variant_images.size > 1
                         variant_images.second
                       end
end

#set_property(property_name, property_value, property_presentation = property_name) ⇒ Object



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'app/models/spree/product.rb', line 510

def set_property(property_name, property_value, property_presentation = property_name)
  property_name = property_name.to_s.parameterize
  ApplicationRecord.transaction do
    # Manual first_or_create to work around Mobility bug
    property = if Property.where(name: property_name).exists?
                 existing_property = Property.where(name: property_name).first
                 existing_property.presentation ||= property_presentation
                 existing_property.save
                 existing_property
               else
                 Property.create(name: property_name, presentation: property_presentation)
               end

    product_property = if ProductProperty.where(product: self, property: property).exists?
                         ProductProperty.where(product: self, property: property).first
                       else
                         ProductProperty.new(product: self, property: property)
                       end

    product_property.value = property_value
    product_property.save!
  end
end

#storefront_descriptionString

Returns the short description for the product

Returns:

  • (String)


375
376
377
# File 'app/models/spree/product.rb', line 375

def storefront_description
  property('short_description') || description
end

#tax_categorySpree::TaxCategory?

Returns tax category for Product

Returns:



381
382
383
# File 'app/models/spree/product.rb', line 381

def tax_category
  @tax_category ||= super || TaxCategory.default
end

#taxons_for_store(store) ⇒ Object



586
587
588
589
590
# File 'app/models/spree/product.rb', line 586

def taxons_for_store(store)
  Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do
    taxons.for_store(store)
  end
end

#to_csv(store = nil) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'app/models/spree/product.rb', line 619

def to_csv(store = nil)
  store ||= stores.default || stores.first
  properties_for_csv ||= Spree::Property.order(:position).flat_map do |property|
    [
      property.name,
      product_properties.find { |pp| pp.property_id == property.id }&.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 = []

  if has_variants?
    variants_including_master.each_with_index do |variant, index|
      csv_lines << Spree::CSV::ProductVariantPresenter.new(self, variant, index, properties_for_csv, taxons_for_csv, store).call
    end
  else
    csv_lines << Spree::CSV::ProductVariantPresenter.new(self, master, 0, properties_for_csv, taxons_for_csv, store).call
  end

  csv_lines
end

#total_on_handObject



538
539
540
541
542
543
544
# File 'app/models/spree/product.rb', line 538

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

#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



492
493
494
# File 'app/models/spree/product.rb', line 492

def variants_and_option_values(current_currency = nil)
  variants.active(current_currency).joins(:option_value_variants)
end