Class: Liquid::Tags::ProductPrice

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
app/lib/liquid/tags/product_price.rb

Overview

Renders a locale-aware product price for a given SKU.

Usage in posts/articles:
product_price sku:TRT120-1.5x52 %
→ "$3.19"

product_price sku:TRT120-1.5x52, format:per_sq_ft, area:78 %
→ "$3.19/sq ft"

Looks up the CatalogItem from the WarmlyYours US or CA catalog
depending on the current locale, and renders the effective price
(sale price when active, otherwise retail price).

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, parse_context) ⇒ ProductPrice

Returns a new instance of ProductPrice.



19
20
21
22
# File 'app/lib/liquid/tags/product_price.rb', line 19

def initialize(tag_name, markup, parse_context)
  super
  @options = parse_options(markup)
end

Instance Method Details

#render(context) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/lib/liquid/tags/product_price.rb', line 24

def render(context)
  sku = @options[:sku]
  return render_error('sku: is required') if sku.blank?

  locale = context['locale']&.to_s || 'en-US'
  catalog_id = canadian_locale?(locale) ? CatalogConstants::CA_CATALOG_ID : CatalogConstants::US_CATALOG_ID
  currency_symbol = canadian_locale?(locale) ? 'C$' : '$'

  catalog_item = CatalogItem.by_skus(sku).where(catalog_id: catalog_id).first
  return render_error("SKU #{sku} not found") unless catalog_item

  price = catalog_item.sale_price_in_effect? ? catalog_item.sale_price : catalog_item.bom_price
  return render_error("No price for #{sku}") unless price&.positive?

  format_price(price, currency_symbol)
end