Module: Spree::ProductsHelper

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

Instance Method Summary collapse

Methods included from BaseHelper

#available_countries, #base_cache_key, #default_image_for_product, #default_image_for_product_or_variant, #display_price, #frontend_available?, #link_to_tracking, #logo, #maximum_quantity, #meta_data, #meta_data_tags, #meta_image_data_tag, #meta_image_url_path, #method_missing, #pretty_time, #seo_url

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spree::BaseHelper

Instance Method Details

#available_status(product) ⇒ Object

will return a human readable string



83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/spree/products_helper.rb', line 83

def available_status(product)
  return Spree.t(:discontinued) if product.discontinued?
  return Spree.t(:deleted) if product.deleted?

  if product.available?
    Spree.t(:available)
  elsif product.available_on&.future?
    Spree.t(:pending_sale)
  else
    Spree.t(:no_available_date_set)
  end
end

#cache_key_for_product(product = @product) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'app/helpers/spree/products_helper.rb', line 66

def cache_key_for_product(product = @product)
  cache_key_elements = common_product_cache_keys
  cache_key_elements += [
    product.cache_key_with_version,
    product.possible_promotions.map(&:cache_key)
  ]

  cache_key_elements.compact.join('/')
end

#cache_key_for_products(products = @products, additional_cache_key = nil) ⇒ Object



60
61
62
63
64
# File 'app/helpers/spree/products_helper.rb', line 60

def cache_key_for_products(products = @products, additional_cache_key = nil)
  max_updated_at = (products.maximum(:updated_at) || Date.today).to_s(:number)
  products_cache_keys = "spree/products/#{products.map(&:id).join('-')}-#{params[:page]}-#{params[:sort_by]}-#{max_updated_at}-#{@taxon&.id}"
  (common_product_cache_keys + [products_cache_keys] + [additional_cache_key]).compact.join('/')
end

#common_product_cache_keysObject



136
137
138
# File 'app/helpers/spree/products_helper.rb', line 136

def common_product_cache_keys
  base_cache_key + price_options_cache_key
end

#default_variant(variants, product) ⇒ Object



34
35
36
# File 'app/helpers/spree/products_helper.rb', line 34

def default_variant(variants, product)
  variants_option_types_presenter(variants, product).default_variant || product.default_variant
end

#limit_descritpion(string) ⇒ Object



76
77
78
79
80
# File 'app/helpers/spree/products_helper.rb', line 76

def limit_descritpion(string)
  return string if string.length <= 450

  string.slice(0..449) + '...'
end

#line_item_description_text(description_text) ⇒ Object



52
53
54
55
56
57
58
# File 'app/helpers/spree/products_helper.rb', line 52

def line_item_description_text(description_text)
  if description_text.present?
    truncate(strip_tags(description_text.gsub('&nbsp;', ' ').squish), length: 100)
  else
    Spree.t(:product_has_no_description)
  end
end

#product_available_in_currency?Boolean

Returns:

  • (Boolean)


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

def product_available_in_currency?
  !(@product_price.nil? || @product_price.zero?)
end

#product_description(product) ⇒ Object

converts line breaks in product description into <p> tags (for html display purposes)



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

def product_description(product)
  description = if Spree::Config[:show_raw_product_description]
                  product.description
                else
                  product.description.to_s.gsub(/(.*?)\r?\n\r?\n/m, '<p>\1</p>')
                end
  description.blank? ? Spree.t(:product_has_no_description) : description
end

#product_images(product, variants) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'app/helpers/spree/products_helper.rb', line 96

def product_images(product, variants)
  if product.variants_and_option_values(current_currency).any?
    variants_without_master_images = variants.reject(&:is_master).map(&:images).flatten

    if variants_without_master_images.any?
      return variants_without_master_images
    end
  end

  variants.map(&:images).flatten
end

#product_variants_matrix(is_product_available_in_currency) ⇒ Object



108
109
110
111
112
113
114
115
# File 'app/helpers/spree/products_helper.rb', line 108

def product_variants_matrix(is_product_available_in_currency)
  Spree::VariantPresenter.new(
    variants: @variants,
    is_product_available_in_currency: is_product_available_in_currency,
    current_currency: current_currency,
    current_price_options: current_price_options
  ).call.to_json
end


117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/spree/products_helper.rb', line 117

def related_products
  return [] unless @product.respond_to?(:has_related_products?) && @product.has_related_products?(:related_products)

  @related_products ||= @product.
                        related_products.
                        includes(
                          :tax_category,
                          master: [
                            :prices,
                            images: { attachment_attachment: :blob },
                          ]
                        ).
                        limit(Spree::Config[:products_per_page])
end

#used_variants_options(variants, product) ⇒ Object



38
39
40
# File 'app/helpers/spree/products_helper.rb', line 38

def used_variants_options(variants, product)
  variants_option_types_presenter(variants, product).options
end

#variant_full_price(variant) ⇒ Object

returns the formatted full price for the variant, if at least one variant price differs from product price



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

def variant_full_price(variant)
  product = variant.product
  unless product.variants.active(current_currency).all? { |v| v.price == product.price }
    Spree::Money.new(variant.price, currency: current_currency).to_html
  end
end

#variant_price(variant) ⇒ Object

returns the formatted price for the specified variant as a full price or a difference depending on configuration



6
7
8
9
10
11
12
# File 'app/helpers/spree/products_helper.rb', line 6

def variant_price(variant)
  if Spree::Config[:show_variant_full_price]
    variant_full_price(variant)
  else
    variant_price_diff(variant)
  end
end

#variant_price_diff(variant) ⇒ Object

returns the formatted price for the specified variant as a difference from product price



15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/spree/products_helper.rb', line 15

def variant_price_diff(variant)
  variant_amount = variant.amount_in(current_currency)
  product_amount = variant.product.amount_in(current_currency)
  return if variant_amount == product_amount || product_amount.nil?

  diff   = variant.amount_in(current_currency) - product_amount
  amount = Spree::Money.new(diff.abs, currency: current_currency).to_html
  label  = diff > 0 ? :add : :subtract
  "(#{Spree.t(label)}: #{amount})".html_safe
end