Class: Spree::Product
- Inherits:
-
Object
- Object
- Spree::Product
- Includes:
- MemoizedData, Metadata, Metafields, 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
Constant Summary collapse
- MEMOIZED_METHODS =
%w[total_on_hand taxonomy_ids taxon_and_ancestors category default_variant_id tax_category default_variant variant_for_images category_taxon 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 ].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.
-
#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” -> […].
-
#category ⇒ Spree::Category, Spree::Taxon
Returns the category for the product If a category association is defined (e.g., belongs_to :category), it will be used Otherwise, falls back to category_taxon for compatibility.
-
#category_taxon ⇒ Spree::Taxon
Returns the category taxon for the product.
-
#default_image ⇒ Spree::Image?
(also: #primary_image)
Returns default Image for Product.
-
#default_variant ⇒ Spree::Variant
Returns default Variant for Product If
track_inventory_levelsis enabled it will try to find the first Variant in stock or backorderable, if there’s none it will return first Variant sorted bypositionattribute Iftrack_inventory_levelsis disabled it will return first Variant sorted bypositionattribute. -
#default_variant_id ⇒ Integer
Returns default Variant ID for Product.
-
#deleted? ⇒ Boolean
use deleted? rather than checking the attribute directly.
-
#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.
- #empty_option_values? ⇒ Boolean
-
#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 Spree::Product#default_image instead.
- #find_or_build_master ⇒ Object
-
#find_variant_with_images ⇒ Spree::Variant?
Finds first variant with images using preloaded data when available.
- #first_available_variant(currency) ⇒ Object
- #first_or_default_variant(currency) ⇒ Object
-
#has_variant_images? ⇒ Boolean
(also: #has_images?)
Returns true if any variant (including master) has images.
-
#has_variants? ⇒ Boolean
Checks if product has variants (non-master variants) Uses variant_count counter cache for performance.
-
#image_count ⇒ Integer
Returns the image count from the variant used for displaying images.
-
#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
- #property(property_name) ⇒ Object
-
#purchasable? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259.
- #remove_property(property_name) ⇒ Object
-
#secondary_image ⇒ Spree::Image?
Returns secondary Image for Product (for hover effects).
- #set_property(property_name, property_value, property_presentation = property_name) ⇒ Object
-
#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
- #total_on_hand ⇒ Object
-
#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 Slugs
Methods included from Webhooks
#send_product_activated_webhook, #send_product_archived_webhook, #send_product_drafted_webhook
Instance Attribute Details
#option_values_hash ⇒ Object
Returns the value of attribute option_values_hash.
212 213 214 |
# File 'app/models/spree/product.rb', line 212 def option_values_hash @option_values_hash end |
#prototype_id ⇒ Object
Adding properties and option types on creation based on a chosen prototype
414 415 416 |
# File 'app/models/spree/product.rb', line 414 def prototype_id @prototype_id end |
Class Method Details
.bulk_auto_match_taxons(store, product_ids) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'app/models/spree/product.rb', line 274 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
508 509 510 511 512 513 |
# File 'app/models/spree/product.rb', line 508 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
434 435 436 437 438 439 440 |
# File 'app/models/spree/product.rb', line 434 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
664 665 666 667 668 669 670 |
# File 'app/models/spree/product.rb', line 664 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
681 682 683 684 685 686 687 688 689 |
# File 'app/models/spree/product.rb', line 681 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
476 477 478 |
# File 'app/models/spree/product.rb', line 476 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
302 303 304 |
# File 'app/models/spree/product.rb', line 302 def backorderable? default_variant.backorderable? || variants.any?(&:backorderable?) end |
#backordered? ⇒ Boolean
determine if any variant (including master) is out of stock and backorderable
496 497 498 |
# File 'app/models/spree/product.rb', line 496 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
585 586 587 588 589 590 591 592 |
# File 'app/models/spree/product.rb', line 585 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
614 615 616 |
# File 'app/models/spree/product.rb', line 614 def brand_name brand&.name end |
#brand_taxon ⇒ Spree::Taxon
Returns the brand taxon for the product
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'app/models/spree/product.rb', line 596 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) }) 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
491 492 493 |
# File 'app/models/spree/product.rb', line 491 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” -> […]
502 503 504 505 506 |
# File 'app/models/spree/product.rb', line 502 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 |
#category ⇒ Spree::Category, Spree::Taxon
Returns the category for the product If a category association is defined (e.g., belongs_to :category), it will be used Otherwise, falls back to category_taxon for compatibility
622 623 624 625 626 627 628 629 |
# File 'app/models/spree/product.rb', line 622 def category if self.class.reflect_on_association(:category) super else Spree::Deprecation.warn('Spree::Product#category is deprecated and will be removed in Spree 5.5. Please use Spree::Product#category_taxon instead.') category_taxon end end |
#category_taxon ⇒ Spree::Taxon
Returns the category taxon for the product
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'app/models/spree/product.rb', line 633 def category_taxon @category_taxon ||= if classification_count.zero? nil elsif 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_image ⇒ Spree::Image? Also known as: primary_image
Returns default Image for Product.
366 367 368 |
# File 'app/models/spree/product.rb', line 366 def default_image variant_for_images&.primary_image 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
331 332 333 334 335 336 337 |
# File 'app/models/spree/product.rb', line 331 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
341 342 343 |
# File 'app/models/spree/product.rb', line 341 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.
469 470 471 |
# File 'app/models/spree/product.rb', line 469 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
677 678 679 |
# File 'app/models/spree/product.rb', line 677 def digital? @digital ||= shipping_category&.includes_digital_shipping_method? end |
#discontinue! ⇒ Object
480 481 482 483 484 |
# File 'app/models/spree/product.rb', line 480 def discontinue! self.discontinue_on = Time.current self.status = 'archived' save(validate: false) end |
#discontinued? ⇒ Boolean
486 487 488 |
# File 'app/models/spree/product.rb', line 486 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
462 463 464 |
# File 'app/models/spree/product.rb', line 462 def duplicate Products::Duplicator.call(product: self) end |
#empty_option_values? ⇒ Boolean
524 525 526 527 528 |
# File 'app/models/spree/product.rb', line 524 def empty_option_values? .empty? || .any? do |opt| opt.option_type.option_values.empty? end end |
#ensure_option_types_exist_for_values_hash ⇒ Object
Ensures option_types and product_option_types exist for keys in option_values_hash
449 450 451 452 453 454 455 456 457 458 |
# File 'app/models/spree/product.rb', line 449 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 Spree::Product#default_image instead.
Backward compatibility for Spree 5.2 and earlier.
372 373 374 375 376 |
# File 'app/models/spree/product.rb', line 372 def featured_image Spree::Deprecation.warn('Spree::Product#featured_image is deprecated and will be removed in Spree 5.5. Please use Spree::Product#default_image instead.') default_image end |
#find_or_build_master ⇒ Object
310 311 312 |
# File 'app/models/spree/product.rb', line 310 def find_or_build_master master || build_master end |
#find_variant_with_images ⇒ Spree::Variant?
Finds first variant with images using preloaded data when available.
395 396 397 398 399 |
# File 'app/models/spree/product.rb', line 395 def find_variant_with_images return variants.find(&:has_images?) if variants.loaded? variants.joins(:images).first end |
#first_available_variant(currency) ⇒ Object
426 427 428 |
# File 'app/models/spree/product.rb', line 426 def first_available_variant(currency) variants.find { |v| v.purchasable? && v.price_in(currency).amount.present? } end |
#first_or_default_variant(currency) ⇒ Object
416 417 418 419 420 421 422 423 424 |
# File 'app/models/spree/product.rb', line 416 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_variant_images? ⇒ Boolean Also known as: has_images?
Returns true if any variant (including master) has images. Uses loaded association when available, otherwise falls back to counter cache.
348 349 350 351 352 |
# File 'app/models/spree/product.rb', line 348 def has_variant_images? return variant_images.any? if association(:variant_images).loaded? total_image_count.positive? end |
#has_variants? ⇒ Boolean
Checks if product has variants (non-master variants) Uses variant_count counter cache for performance
317 318 319 320 321 |
# File 'app/models/spree/product.rb', line 317 def has_variants? return variants.size.positive? if variants.loaded? variant_count.positive? end |
#image_count ⇒ Integer
Returns the image count from the variant used for displaying images.
389 390 391 |
# File 'app/models/spree/product.rb', line 389 def image_count variant_for_images&.image_count || 0 end |
#in_stock? ⇒ Boolean
Can’t use short form block syntax due to github.com/Netflix/fast_jsonapi/issues/259
297 298 299 |
# File 'app/models/spree/product.rb', line 297 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
444 445 446 |
# File 'app/models/spree/product.rb', line 444 def lowest_price(currency) prices_including_master.find_all { |p| p.currency == currency }.min_by(&:amount) end |
#main_taxon ⇒ Object
650 651 652 653 654 |
# File 'app/models/spree/product.rb', line 650 def main_taxon return if classification_count.zero? @main_taxon ||= category_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.
577 578 579 |
# File 'app/models/spree/product.rb', line 577 def master super || variants_including_master.with_deleted.find_by(is_master: true) end |
#on_sale?(currency) ⇒ Boolean
306 307 308 |
# File 'app/models/spree/product.rb', line 306 def on_sale?(currency) prices_including_master.find_all { |p| p.currency == currency }.any?(&:discounted?) end |
#price_varies?(currency) ⇒ Boolean
430 431 432 |
# File 'app/models/spree/product.rb', line 430 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
530 531 532 533 534 535 536 |
# File 'app/models/spree/product.rb', line 530 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
292 293 294 |
# File 'app/models/spree/product.rb', line 292 def purchasable? @purchasable ||= default_variant.purchasable? || variants.in_stock_or_backorderable.any? end |
#remove_property(property_name) ⇒ Object
562 563 564 |
# File 'app/models/spree/product.rb', line 562 def remove_property(property_name) product_properties.joins(:property).find_by(spree_properties: { name: property_name.parameterize })&.destroy end |
#secondary_image ⇒ Spree::Image?
Returns secondary Image for Product (for hover effects).
380 381 382 |
# File 'app/models/spree/product.rb', line 380 def secondary_image variant_for_images&.secondary_image end |
#set_property(property_name, property_value, property_presentation = property_name) ⇒ Object
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'app/models/spree/product.rb', line 538 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_description ⇒ String
Returns the short description for the product
403 404 405 |
# File 'app/models/spree/product.rb', line 403 def storefront_description property('short_description') || description end |
#tax_category ⇒ Spree::TaxCategory?
Returns tax category for Product
409 410 411 |
# File 'app/models/spree/product.rb', line 409 def tax_category @tax_category ||= super || TaxCategory.default end |
#taxons_for_store(store) ⇒ Object
656 657 658 659 660 661 662 |
# File 'app/models/spree/product.rb', line 656 def taxons_for_store(store) return if classification_count.zero? Rails.cache.fetch("#{cache_key_with_version}/taxons-per-store/#{store.id}") do taxons.loaded? ? taxons.find_all { |taxon| taxon.taxonomy.store_id == store.id } : taxons.for_store(store) end end |
#to_csv(store = nil) ⇒ Object
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'app/models/spree/product.rb', line 691 def to_csv(store = nil) store ||= stores.default || stores.first properties_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::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 = [] 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_hand ⇒ Object
566 567 568 569 570 571 572 |
# File 'app/models/spree/product.rb', line 566 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 |
#variant_for_images ⇒ Spree::Variant?
Returns the variant that should be used for displaying images. Priority: master > default_variant > first variant with images
360 361 362 |
# File 'app/models/spree/product.rb', line 360 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
520 521 522 |
# File 'app/models/spree/product.rb', line 520 def variants_and_option_values(current_currency = nil) variants.active(current_currency).joins(:option_value_variants) end |