Module: ProductsHelper

Defined in:
app/helpers/products_helper.rb

Instance Method Summary collapse

Instance Method Details

#customer_has_tier_pricing?Boolean

Check if current customer has tier pricing

Returns:

  • (Boolean)


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

def customer_has_tier_pricing?
  customer = &.customer
  return false unless customer

  customer.pricing_program_discount.to_f.positive?
end

Cloudflare Stream iframe when MP4 is not used as Fancybox href (HTML5 prefers direct file per Fancybox video guide)



57
58
59
60
61
62
63
# File 'app/helpers/products_helper.rb', line 57

def product_gallery_cloudflare_stream_iframe_url(video)
  return unless video.respond_to?(:is_cloudflare?) && video.is_cloudflare?

  poster = video.gallery_square_poster_url(width: 1200, height: 1200) ||
           "#{video.cf_poster_url}?width=1200&height=1200&fit=clip"
  "#{CF_STREAM_URL}/#{video.cloudflare_uid}/iframe?poster=#{CGI.escape(poster)}"
end

#product_line_page_keywords(product_line = nil) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'app/helpers/products_helper.rb', line 2

def product_line_page_keywords(product_line = nil)
  return unless product_line ||= @product_line
  return product_line.seo_keywords if product_line.seo_keywords.present?

  plr = product_line.get_first_reviewable
  return plr.seo_keywords if plr and plr.seo_keywords.present?

  keywords = product_line.generate_full_name_array + [product_line.public_name]
  keywords.map(&:presence).compact.join(', ')
end

#tier_pricing_for_product(product, customer: nil) ⇒ Hash?

Calculate tier-discounted price for a product, honoring max_discount cap
Returns nil if no tier pricing applies

Parameters:

  • product (ViewProductCatalog, ProductCatalogPresenter)

    The product to price

  • customer (Customer, nil) (defaults to: nil)

    Optional customer (defaults to current_account.customer)

Returns:

  • (Hash, nil)

    { price:, formatted:, original:, original_formatted:, savings:, discount_pct: } or nil



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/products_helper.rb', line 18

def tier_pricing_for_product(product, customer: nil)
  customer ||= &.customer
  return nil unless customer

  tier_discount_pct = customer.pricing_program_discount.to_f
  return nil unless tier_discount_pct.positive?

  original_price = product.original_price || product.price
  catalog_item = product.respond_to?(:catalog_item) ? product.catalog_item : nil
  max_discount_pct = catalog_item&.max_discount || 100

  # Apply the lesser of tier discount or max discount cap
  effective_discount_pct = [tier_discount_pct, max_discount_pct].min
  effective_discount_factor = effective_discount_pct / 100.0
  tier_price = (original_price * (1 - effective_discount_factor)).round(2)
  savings = original_price - tier_price

  currency_symbol = product.respond_to?(:currency_symbol) ? product.currency_symbol : '$'

  {
    price: tier_price,
    formatted: number_to_currency(tier_price, unit: currency_symbol),
    original: original_price,
    original_formatted: number_to_currency(original_price, unit: currency_symbol),
    savings: savings,
    discount_pct: effective_discount_pct.round(0),
    is_capped: tier_discount_pct > max_discount_pct
  }
end