Module: Spree::ProductsHelper

Includes:
BaseHelper
Defined in:
app/helpers/spree/products_helper.rb

Instance Method Summary collapse

Instance Method Details

#brand_name(product) ⇒ Object



27
28
29
# File 'app/helpers/spree/products_helper.rb', line 27

def brand_name(product)
  product.brand&.name || product.vendor&.display_name
end

#image_alt(image) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'app/helpers/spree/products_helper.rb', line 105

def image_alt(image)
  @memoized_image_alts ||= {}
  key = image.id

  @memoized_image_alts[key] ||=
    image.alt.presence ||
    (image.viewable.descriptive_name if image.viewable_type == 'Spree::Variant') ||
    @product.name
end

#option_type_colors_preview_styles(option_type) ⇒ Object



199
200
201
202
203
# File 'app/helpers/spree/products_helper.rb', line 199

def option_type_colors_preview_styles(option_type)
  return unless option_type.color?

  Spree::ColorsPreviewStylesPresenter.new(option_type.option_values.map { |ov| { name: ov.name, filter_name: ov.name } }).to_s
end

#product_breadcrumb_taxons(product) ⇒ Object



148
149
150
151
152
153
# File 'app/helpers/spree/products_helper.rb', line 148

def product_breadcrumb_taxons(product)
  return Spree::Taxon.none if product.main_taxon.blank?

  # using find_all as we already iterate over the taxons in #product_json_ld_breadcrumbs
  product.main_taxon.self_and_ancestors.find_all { |taxon| taxon.depth != 0 }
end

#product_cache_key(product) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/spree/products_helper.rb', line 5

def product_cache_key(product)
  [
    current_theme,
    current_store,
    try_spree_current_user,
    current_currency,
    I18n.locale,
    product.cache_key_with_version,
    product.available?,
    product.discontinued?,
  ]
end

#product_json_ld_breadcrumbs(product) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/helpers/spree/products_helper.rb', line 165

def product_json_ld_breadcrumbs(product)
  json_ld = {
    '@context' => 'https://schema.org',
    '@type' => 'BreadcrumbList',
    'itemListElement' => [
      {
        '@type': 'ListItem',
        'position' => 1,
        'name' => 'Homepage',
        'item' => spree.root_url(host: current_store.url_or_custom_domain)
      }
    ]
  }

  if product.main_taxon.present?
    product.main_taxon.self_and_ancestors.each_with_index do |taxon, index|
      json_ld['itemListElement'] << {
        '@type' => 'ListItem',
        'position' => index + 2,
        'name' => taxon.name,
        'item' => spree.nested_taxons_url(taxon, host: current_store.url_or_custom_domain)
      }
    end
  end

  json_ld['itemListElement'] << {
    '@type' => 'ListItem',
    'position' => json_ld['itemListElement'].length + 1,
    'name' => product.name
  }

  json_ld
end

#product_list_json_ld_elements(product_slugs) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'app/helpers/spree/products_helper.rb', line 155

def product_list_json_ld_elements(product_slugs)
  product_slugs.each_with_index.map do |product_slug, index|
    {
      '@type' => 'ListItem',
      'position' => index + 1,
      'url' => spree.product_url(product_slug)
    }
  end
end


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/spree/products_helper.rb', line 77

def product_media_gallery_images(product, selected_variant:, variant_from_options:, options_param_name: :options)
  images = selected_variant&.images&.to_a || []
  images ||= variant_from_options&.images&.to_a if images.empty?

  if images.compact.empty?
    first_option_type = product.option_types.first

    if first_option_type.present?
      variant_for_first_option = product_variant_for_selected_option(
        first_option_type, product: product, selected_variant: selected_variant, options_param_name: options_param_name
      )

      images << variant_for_first_option.images.to_a if variant_for_first_option.present?
    end
  end

  images << product.master&.images&.to_a
  images << product.default_image if images.flatten.compact.empty?

  images.flatten.compact.uniq(&:id)
end

#product_not_selected_options(product, selected_variant, options_param_name: :options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/spree/products_helper.rb', line 31

def product_not_selected_options(product, selected_variant, options_param_name: :options)
  product.option_types.map do |option_type|
    if product_selected_option_for_option(
      option_type,
      product: product,
      selected_variant: selected_variant,
      options_param_name: options_param_name
    ).present?
      next
    else
      option_type
    end
  end.compact_blank
end

#product_option_values_for_option(option_type, product:) ⇒ Object



71
72
73
74
75
# File 'app/helpers/spree/products_helper.rb', line 71

def product_option_values_for_option(option_type, product:)
  product.option_values.includes(:option_type).
    find_all { |option_value| option_value.option_type_id == option_type.id }.
    uniq { |ov| [ov.name, ov.option_type&.name] }
end

#product_selected_option_for_option(option_type, product:, selected_variant: nil, options_param_name: :options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/spree/products_helper.rb', line 46

def product_selected_option_for_option(option_type, product:, selected_variant: nil, options_param_name: :options)
  @memoized_values ||= {}
  memoized_key = "#{product.id}-#{selected_variant&.id}-#{option_type.id}-#{options_param_name}"

  product_selected_options_hash = if params[options_param_name].present?
                                    params[options_param_name].split(',').to_h do |option|
                                      key, *value = option.split(':')
                                      [key, value.join(':')]
                                    end
                                  else
                                    {}
                                  end

  @memoized_values[memoized_key] ||=
    if selected_variant.present?
      selected_variant.option_values.find { |ov| ov.option_type_id == option_type.id }
    elsif product_selected_options_hash.present? && product_selected_options_hash[option_type.id.to_s].present? # user selected variant which is not available
      option_type.option_values.find { |v| v.name == product_selected_options_hash[option_type.id.to_s] }
    elsif option_type.color? && (available_variant = product.first_available_variant(current_currency))
      available_variant.option_values.find { |ov| ov.option_type_id == option_type.id }
    elsif option_type.color? && product.first_available_variant(current_currency).nil?
      product.variants.first&.option_values&.find { |ov| ov.option_type_id == option_type.id }
    end
end

#product_variant_for_selected_option(option_type, product:, selected_variant:, options_param_name: :options) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'app/helpers/spree/products_helper.rb', line 115

def product_variant_for_selected_option(option_type, product:, selected_variant:, options_param_name: :options)
  selected_value_for_option = product_selected_option_for_option(
    option_type,
    product: product,
    selected_variant: selected_variant,
    options_param_name: options_param_name
  )
  return unless selected_value_for_option.present?

  product.variants.with_option_value(option_type.name, selected_value_for_option.name).first
end

#products_selected_filters_countObject



127
128
129
# File 'app/helpers/spree/products_helper.rb', line 127

def products_selected_filters_count
  params[:filters].present? ? params[:filters].split(',').count : 0
end

#products_sortObject



131
132
133
# File 'app/helpers/spree/products_helper.rb', line 131

def products_sort
  @products_sort ||= permitted_products_params[:sort_by]&.strip_html_tags.presence || default_products_sort
end

#should_display_compare_at_price?(product_or_variant) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'app/helpers/spree/products_helper.rb', line 18

def should_display_compare_at_price?(product_or_variant)
  price = product_or_variant.price_in(current_currency)
  price.compare_at_amount.present? && (price.compare_at_amount > price.amount)
end

#taxons_sort_optionsObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/spree/products_helper.rb', line 135

def taxons_sort_options
  @taxons_sort_options ||= [
    { name: Spree.t('products_sort_options.relevance'), value: 'manual' },
    { name: Spree.t('products_sort_options.best_selling'), value: 'best-selling' },
    { name: Spree.t('products_sort_options.name_a_z'), value: 'name-a-z' },
    { name: Spree.t('products_sort_options.name_z_a'), value: 'name-z-a' },
    { name: Spree.t('products_sort_options.price_low_to_high'), value: 'price-low-to-high' },
    { name: Spree.t('products_sort_options.price_high_to_low'), value: 'price-high-to-low' },
    { name: Spree.t('products_sort_options.newest_first'), value: 'newest-first' },
    { name: Spree.t('products_sort_options.oldest_first'), value: 'oldest-first' }
  ]
end


99
100
101
102
103
# File 'app/helpers/spree/products_helper.rb', line 99

def variant_featured_image(variant)
  return nil unless variant

  variant.images.first
end

#weeks_online(product) ⇒ Object



23
24
25
# File 'app/helpers/spree/products_helper.rb', line 23

def weeks_online(product)
  (Time.current - product.activated_at.in_time_zone(current_store.preferred_timezone)).seconds.in_weeks.to_i.abs
end