Module: Spree::ProductsHelper

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

Instance Method Summary collapse

Methods included from BaseHelper

#all_countries, #available_countries, #base_cache_key, #default_image_for_product, #default_image_for_product_or_variant, #display_compare_at_price, #display_price, #frontend_available?, #link_to_tracking, #logo, #maximum_quantity, #meta_data, #meta_data_tags, #method_missing, #object, #og_meta_data, #og_meta_data_tags, #pretty_date, #pretty_time, #seo_url, #spree_class_name_as_path, #spree_favicon_path, #spree_resource_path, #spree_storefront_resource_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



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

def available_status(product)
  Spree::Deprecation.warn(<<-DEPRECATION, caller)
    `Spree::ProductsHelper#available_status` method from spree/core is deprecated and will be removed.
    Please use `Spree::Admin::ProductsHelper#available_status` from spree_backend instead.
  DEPRECATION

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

  if product.available?
    Spree.t(:active)
  elsif product.make_active_at&.future?
    Spree.t(:pending_sale)
  else
    Spree.t(:draft)
  end
end

#cache_key_for_product(product = @product) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'app/helpers/spree/products_helper.rb', line 61

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



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

def cache_key_for_products(products = @products, additional_cache_key = nil)
  max_updated_at = (products.except(:group, :order).maximum(:updated_at) || Date.today).to_formatted_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



157
158
159
# File 'app/helpers/spree/products_helper.rb', line 157

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_description(string) ⇒ Object



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

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

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

#line_item_description_text(description_text) ⇒ Object



47
48
49
50
51
52
53
# File 'app/helpers/spree/products_helper.rb', line 47

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)


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

def product_available_in_currency?
  !(@product_price.nil? || @product_price.zero?)
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_relation_typesObject



118
119
120
# File 'app/helpers/spree/products_helper.rb', line 118

def product_relation_types
  @product_relation_types ||= @product.respond_to?(:relation_types) ? @product.relation_types : []
end

#product_relations_by_type(relation_type) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/spree/products_helper.rb', line 122

def product_relations_by_type(relation_type)
  return [] if product_relation_types.none? || !@product.respond_to?(:relations)

  product_ids = @product.relations.where(relation_type: relation_type).pluck(:related_to_id).uniq

  return [] if product_ids.empty?

  current_store.products.
    available.not_discontinued.distinct.
    where(id: product_ids).
    includes(
      :tax_category,
      master: [
        :prices,
        { images: { attachment_attachment: :blob } },
      ]
    ).
    limit(Spree::Config[:products_per_page])
end

#product_variants_matrix(is_product_available_in_currency) ⇒ Object



108
109
110
111
112
113
114
115
116
# 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,
    current_store: current_store
  ).call.to_json
end


142
143
144
145
146
147
148
149
150
151
# File 'app/helpers/spree/products_helper.rb', line 142

def related_products
  Spree::Deprecation.warn(<<-DEPRECATION, caller)
    ProductsHelper#related_products is deprecated and will be removed in Spree 5.0.
    Please use ProductsHelper#relations from now on.
  DEPRECATION

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

  @related_products ||= relations_by_type('related_products')
end

#should_display_compare_at_price?(default_variant) ⇒ Boolean

Returns:

  • (Boolean)


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

def should_display_compare_at_price?(default_variant)
  default_variant_price = default_variant.price_in(current_currency)
  default_variant_price.compare_at_amount.present? && (default_variant_price.compare_at_amount > default_variant_price.amount)
end

#used_variants_options(variants, product) ⇒ Object



43
44
45
# File 'app/helpers/spree/products_helper.rb', line 43

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