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
-
#pricing_context_for_variant(product_or_variant, **options) ⇒ Spree::Pricing::Context
Builds a pricing context for a variant with current request context.
- #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_json_ld_data(product, selected_variant: nil) ⇒ Object
- #product_json_ld_variant_offer(product, variant) ⇒ Object
-
#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
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
268 269 270 271 272 |
# File 'app/helpers/spree/products_helper.rb', line 268 def option_type_colors_preview_styles(option_type) return unless option_type.color_swatch? 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
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, **) variant = product_or_variant.is_a?(Spree::Product) ? product_or_variant.default_variant : product_or_variant Spree::Pricing::Context.new( variant: variant, currency: [:currency] || current_currency, store: [:store] || current_store, zone: [:zone] || current_order&.tax_zone || current_store.checkout_zone, user: [:user] || try_spree_current_user, quantity: [:quantity], date: [:date], order: [: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) 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.
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 = { '@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_json_ld_data(product, selected_variant: nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'app/helpers/spree/products_helper.rb', line 228 def product_json_ld_data(product, selected_variant: nil) first_or_default_variant = product.first_or_default_variant(current_currency) data = { '@context' => 'https://schema.org/', '@type' => 'Product', 'name' => product.name, 'url' => spree.product_url(product, host: current_store.url_or_custom_domain) } data['image'] = [spree_image_url(product.primary_media, variant: :large)] if product.has_images? data['description'] = (product.description) if product.description.present? sku_variant = product.has_variants? ? selected_variant : first_or_default_variant data['sku'] = sku_variant.sku if sku_variant&.sku.present? if product.brand_taxon data['brand'] = { '@type' => 'Brand', 'name' => product.brand_taxon.name } end data['offers'] = if product.has_variants? product.variants.map { |variant| product_json_ld_variant_offer(product, variant) } else [product_json_ld_variant_offer(product, first_or_default_variant)] end data end |
#product_json_ld_variant_offer(product, variant) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'app/helpers/spree/products_helper.rb', line 254 def product_json_ld_variant_offer(product, variant) Rails.cache.fetch(['json-ld-variant-hash', *spree_base_cache_key, variant.cache_key_with_version]) do offer = { '@type' => 'Offer', 'availability' => "http://schema.org/#{variant.available? ? 'InStock' : 'OutOfStock'}", 'price' => variant.amount_in(current_currency), 'priceCurrency' => current_currency, 'url' => spree.product_url(product, variant_id: variant.id, host: current_store.url_or_custom_domain) } offer['sku'] = variant.sku if variant.sku.present? offer end end |
#product_list_json_ld_elements(product_slugs) ⇒ Array<Hash>
Generates the JSON-LD elements for a list of products.
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 |
#product_media_gallery_images(product, selected_variant:, variant_from_options:, options_param_name: :options) ⇒ Object
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 ||= &.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.primary_media 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, 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
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
274 275 276 |
# File 'app/helpers/spree/products_helper.rb', line 274 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}-#{}" = 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_swatch? && (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_swatch? && 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: ) 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
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_sort ⇒ Object
152 153 154 |
# File 'app/helpers/spree/products_helper.rb', line 152 def products_sort @products_sort ||= permitted_products_params[:sort_by]&..presence || default_products_sort end |
#should_display_compare_at_price?(product_or_variant) ⇒ 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_options ⇒ Object
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 ||= [ { 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
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 |