Class: Spree::Product
- Inherits:
-
Object
- Object
- Spree::Product
- Includes:
- Linkable, 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 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 ].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: #featured_image)
Returns default Image for Product.
-
#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.
-
#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.
- #find_or_build_master ⇒ Object
- #first_available_variant(currency) ⇒ Object
- #first_or_default_variant(currency) ⇒ Object
-
#has_variants? ⇒ Boolean
the master variant is not a member of the variants array.
-
#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
- #page_builder_url ⇒ Object
- #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.
- #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
-
#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.
217 218 219 |
# File 'app/models/spree/product.rb', line 217 def option_values_hash @option_values_hash end |
#prototype_id ⇒ Object
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
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
628 629 630 631 632 633 634 |
# File 'app/models/spree/product.rb', line 628 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
645 646 647 648 649 650 651 652 653 |
# File 'app/models/spree/product.rb', line 645 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
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
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
468 469 470 |
# File 'app/models/spree/product.rb', line 468 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
557 558 559 560 561 562 563 564 |
# File 'app/models/spree/product.rb', line 557 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 6. Please use Spree::Product#brand_taxon instead.') brand_taxon end end |
#brand_name ⇒ String
Returns the brand name for the product
584 585 586 |
# File 'app/models/spree/product.rb', line 584 def brand_name brand&.name end |
#brand_taxon ⇒ Spree::Taxon
Returns the brand taxon for the product
568 569 570 571 572 573 574 575 576 577 578 579 580 |
# File 'app/models/spree/product.rb', line 568 def brand_taxon @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
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 |
#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
592 593 594 595 596 597 598 599 |
# File 'app/models/spree/product.rb', line 592 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 6. Please use Spree::Product#category_taxon instead.') category_taxon end end |
#category_taxon ⇒ Spree::Taxon
Returns the category taxon for the product
603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
# File 'app/models/spree/product.rb', line 603 def category_taxon @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_image ⇒ Spree::Image Also known as: featured_image
Returns default Image for Product
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_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
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_id ⇒ Integer
Returns default Variant ID for Product
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.
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
641 642 643 |
# File 'app/models/spree/product.rb', line 641 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
458 459 460 |
# File 'app/models/spree/product.rb', line 458 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
434 435 436 |
# File 'app/models/spree/product.rb', line 434 def duplicate Products::Duplicator.call(product: self) end |
#empty_option_values? ⇒ Boolean
496 497 498 499 500 |
# File 'app/models/spree/product.rb', line 496 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
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_master ⇒ Object
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
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
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_taxon ⇒ Object
618 619 620 |
# File 'app/models/spree/product.rb', line 618 def 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.
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
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_url ⇒ Object
686 687 688 689 690 |
# File 'app/models/spree/product.rb', line 686 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
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
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_image ⇒ Spree::Image
Returns secondary Image for Product
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_description ⇒ String
Returns the short description for the product
375 376 377 |
# File 'app/models/spree/product.rb', line 375 def storefront_description property('short_description') || description end |
#tax_category ⇒ Spree::TaxCategory?
Returns tax category for Product
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
622 623 624 625 626 |
# File 'app/models/spree/product.rb', line 622 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
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
# File 'app/models/spree/product.rb', line 655 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
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 |