Module: Spree::ProductsHelper

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

Instance Method Summary collapse

Instance Method Details

#image_alt(image) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'app/helpers/spree/products_helper.rb', line 126

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



228
229
230
231
232
# File 'app/helpers/spree/products_helper.rb', line 228

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

#pricing_context_for_variant(product_or_variant, **options) ⇒ Spree::Pricing::Context

Builds a pricing context for a variant with current request context

Parameters:

  • product_or_variant (Spree::Product, Spree::Variant)

    The product or variant to build context for

  • options (Hash)

    Optional overrides for context attributes

Options Hash (**options):

  • :currency (String)

    Override the currency (defaults to current_currency)

  • :store (Spree::Store)

    Override the store (defaults to current_store)

  • :zone (Spree::Zone)

    Override the zone (defaults to current_order.tax_zone || current_store.checkout_zone)

  • :user (Spree::User)

    Override the user (defaults to try_spree_current_user)

  • :quantity (Integer)

    Specify quantity for volume pricing

  • :date (Time)

    Specify date for date-based pricing

  • :order (Spree::Order)

    Specify the order

Returns:

  • (Spree::Pricing::Context)

    The pricing context



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

def pricing_context_for_variant(product_or_variant, **options)
  variant = product_or_variant.is_a?(Spree::Product) ? product_or_variant.default_variant : product_or_variant

  Spree::Pricing::Context.new(
    variant: variant,
    currency: options[:currency] || current_currency,
    store: options[:store] || current_store,
    zone: options[:zone] || current_order&.tax_zone || current_store.checkout_zone,
    user: options[:user] || try_spree_current_user,
    quantity: options[:quantity],
    date: options[:date],
    order: options[:order] || current_order
  )
end

#product_breadcrumb_taxons(product) ⇒ Object



169
170
171
172
173
174
# File 'app/helpers/spree/products_helper.rb', line 169

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.includes(:translations).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) ⇒ Hash

Generates the JSON-LD breadcrumbs for a product.

Parameters:

  • product (Spree::Product)

    The product to generate breadcrumbs for

Returns:

  • (Hash)

    The JSON-LD breadcrumbs



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/helpers/spree/products_helper.rb', line 194

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) ⇒ Array<Hash>

Generates the JSON-LD elements for a list of products.

Parameters:

  • product_slugs (Array<String>)

    The slugs of the products to generate elements for

Returns:

  • (Array<Hash>)

    The JSON-LD elements



180
181
182
183
184
185
186
187
188
# File 'app/helpers/spree/products_helper.rb', line 180

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, host: current_store.url_or_custom_domain)
    }
  end
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/spree/products_helper.rb', line 98

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/spree/products_helper.rb', line 52

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



92
93
94
95
96
# File 'app/helpers/spree/products_helper.rb', line 92

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_properties(product) ⇒ Object



234
235
236
# File 'app/helpers/spree/products_helper.rb', line 234

def product_properties(product)
  product.product_properties.joins(:property).merge(Spree::Property.available_on_front_end).sort_by_property_position
end

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/spree/products_helper.rb', line 67

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



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

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



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

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

#products_sortObject



152
153
154
# File 'app/helpers/spree/products_helper.rb', line 152

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)


45
46
47
48
49
50
# File 'app/helpers/spree/products_helper.rb', line 45

def should_display_compare_at_price?(product_or_variant)
  variant = product_or_variant.is_a?(Spree::Product) ? product_or_variant.default_variant : product_or_variant
  context = pricing_context_for_variant(variant)
  price = variant.price_for(context)
  price.compare_at_amount.present? && (price.compare_at_amount > price.amount)
end

#taxons_sort_optionsObject



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

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


120
121
122
123
124
# File 'app/helpers/spree/products_helper.rb', line 120

def variant_featured_image(variant)
  return nil unless variant

  variant.images.first
end