Module: Spree::ProductsHelper
- Includes:
- BaseHelper
- Defined in:
- app/helpers/spree/products_helper.rb
Instance Method Summary collapse
- #image_alt(image) ⇒ Object
- #option_type_colors_preview_styles(option_type) ⇒ Object
- #product_breadcrumb_taxons(product) ⇒ Object
- #product_cache_key(product) ⇒ Object
-
#product_json_ld_breadcrumbs(product) ⇒ Hash
Generates the JSON-LD breadcrumbs for a product.
-
#product_list_json_ld_elements(product_slugs) ⇒ Array<Hash>
Generates the JSON-LD elements for a list of products.
- #product_media_gallery_images(product, selected_variant:, variant_from_options:, options_param_name: :options) ⇒ Object
- #product_not_selected_options(product, selected_variant, options_param_name: :options) ⇒ Object
- #product_option_values_for_option(option_type, product:) ⇒ Object
- #product_properties(product) ⇒ Object
- #product_selected_option_for_option(option_type, product:, selected_variant: nil, options_param_name: :options) ⇒ Object
- #product_variant_for_selected_option(option_type, product:, selected_variant:, options_param_name: :options) ⇒ Object
- #products_selected_filters_count ⇒ Object
- #products_sort ⇒ Object
- #should_display_compare_at_price?(product_or_variant) ⇒ Boolean
- #taxons_sort_options ⇒ Object
- #variant_featured_image(variant) ⇒ Object
- #weeks_online(product) ⇒ Object
Instance Method Details
#image_alt(image) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'app/helpers/spree/products_helper.rb', line 103 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
205 206 207 208 209 |
# File 'app/helpers/spree/products_helper.rb', line 205 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
146 147 148 149 150 151 |
# File 'app/helpers/spree/products_helper.rb', line 146 def (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.
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 198 199 200 201 202 203 |
# File 'app/helpers/spree/products_helper.rb', line 171 def (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.
157 158 159 160 161 162 163 164 165 |
# File 'app/helpers/spree/products_helper.rb', line 157 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 |
#product_media_gallery_images(product, selected_variant:, variant_from_options:, options_param_name: :options) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/helpers/spree/products_helper.rb', line 75 def product_media_gallery_images(product, selected_variant:, variant_from_options:, options_param_name: :options) images = selected_variant&.images&.to_a || [] images ||= &.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: ) 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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/spree/products_helper.rb', line 29 def (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: ).present? next else option_type end end.compact_blank end |
#product_option_values_for_option(option_type, product:) ⇒ Object
69 70 71 72 73 |
# File 'app/helpers/spree/products_helper.rb', line 69 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
211 212 213 |
# File 'app/helpers/spree/products_helper.rb', line 211 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/helpers/spree/products_helper.rb', line 44 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}-#{}" = if params[].present? params[].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 .present? && [option_type.id.to_s].present? # user selected variant which is not available option_type.option_values.find { |v| v.name == [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
113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/helpers/spree/products_helper.rb', line 113 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: ) 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_count ⇒ Object
125 126 127 |
# File 'app/helpers/spree/products_helper.rb', line 125 def products_selected_filters_count params[:filters].present? ? params[:filters].split(',').count : 0 end |
#products_sort ⇒ Object
129 130 131 |
# File 'app/helpers/spree/products_helper.rb', line 129 def products_sort @products_sort ||= permitted_products_params[:sort_by]&..presence || default_products_sort end |
#should_display_compare_at_price?(product_or_variant) ⇒ 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_options ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/helpers/spree/products_helper.rb', line 133 def @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 |
#variant_featured_image(variant) ⇒ Object
97 98 99 100 101 |
# File 'app/helpers/spree/products_helper.rb', line 97 def variant_featured_image(variant) return nil unless variant variant.images.first end |
#weeks_online(product) ⇒ Object
23 24 25 26 27 |
# File 'app/helpers/spree/products_helper.rb', line 23 def weeks_online(product) Spree::Deprecation.warn('weeks_online is deprecated and will be removed in Spree 5.2') (Time.current - product.activated_at.in_time_zone(current_store.preferred_timezone)).seconds.in_weeks.to_i.abs end |