Class: SolidusRecommendations::Recommendable::Products

Inherits:
Base
  • Object
show all
Defined in:
lib/solidus_recommendations/recommendable/products.rb

Overview

Provides methods to get recommended products based on certain criteria such as “Bought Together” or “Users also bought”

Constant Summary collapse

ACCEPTED_INDICES =

Acceptable indices to pass as index option to methods

[:user, :order]

Instance Method Summary collapse

Instance Method Details

#get(product, options = {}) ⇒ Array<Spree::Product]

Gets recommended products

Examples:

Get recommended products based on user index.

product = Spree::Product.find(4)
recommended = SolidusRecommendations::Client.new
recommended.products.get(product)

# Good for getting recommendations based on what other users have
# have purchased.

Get recommended products based on order index.

product = Spree::Product.find(4)
recommmended = SolidusRecommendations::Client.new
recommended.products.get(product, index: :order)

# Good for getting recommendations based on what others checkout with
# frequently.

Parameters:

  • product (Spree::Product, Integer)

    The product to base recommendations off of.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :index (Symbol, String)

    The index to perform aggregation on. (*Defaults to :user*)

  • :size (Integer)

    The number of recommendations to return. (*Defaults to 10*)

Returns:

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/solidus_recommendations/recommendable/products.rb', line 40

def get(product, options = {})
  return [] if product.nil?

  options.deep_symbolize_keys!
  index = options.delete(:index) || :user
  size = options.delete(:size) || 10

  # If index is not supported then we will get a weird undefined
  # method error. This error will make more sense.
  raise Errors::NonSupportedIndex unless ACCEPTED_INDICES.include?(index.to_sym)

  product = convert_to_id([product]).first
  aggs = send("#{index}_index_significant_terms", product, size)

  from_significant_terms(aggs, [product], Spree::Product)
end