Class: Plugins::Ecommerce::ProductDecorator
- Inherits:
-
CamaleonCms::PostDecorator
- Object
- CamaleonCms::PostDecorator
- Plugins::Ecommerce::ProductDecorator
- Defined in:
- app/decorators/plugins/ecommerce/product_decorator.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#can_added?(qty, variation_id = nil) ⇒ Boolean
check if there are enough products to be purchased.
- #eco_featured? ⇒ Boolean
-
#get_default_variation ⇒ Object
return the first variation of a product.
-
#get_variation(variation_id) ⇒ Object
return a product variation by id.
- #in_stock?(variation_id = nil) ⇒ Boolean
-
#is_variation_product? ⇒ Boolean
check if current product is a variation product.
-
#map_variations_the_qty_real ⇒ Object
return (Hash) all variations qty for each variation, sample: 10, 5: 2, 3: 0.
- #price(variation_id = nil) ⇒ Object
- #sku(variation_id = nil) ⇒ Object
- #tax(variation_id = nil) ⇒ Object
- #the_featured_status ⇒ Object
- #the_photos ⇒ Object
- #the_price(variation_id = nil) ⇒ Object
- #the_qty(variation_id = nil) ⇒ Object
-
#the_qty_real(variation_id = nil) ⇒ Object
return the total (Integer) of products available to sell (doesn’t include the qty of the current cart).
- #the_sku(variation_id = nil) ⇒ Object
- #the_stock_status(variation_id = nil) ⇒ Object
- #the_tax(variation_id = nil) ⇒ Object
-
#the_variation_title(variation_id = nil) ⇒ Object
return the title for variation prefixed with the title of the product.
- #the_weight(variation_id = nil) ⇒ Object
-
#valid_variation?(variation_id = nil) ⇒ Boolean
verify if current product needs a variation id to be purchased return true/false.
- #weight(variation_id = nil) ⇒ Object
Class Method Details
.object_class_name ⇒ Object
119 120 121 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 119 def self.object_class_name 'CamaleonCms::Post' end |
Instance Method Details
#can_added?(qty, variation_id = nil) ⇒ Boolean
check if there are enough products to be purchased
115 116 117 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 115 def can_added?(qty, variation_id = nil) (the_qty_real(variation_id) - qty).to_i >= 0 end |
#eco_featured? ⇒ Boolean
102 103 104 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 102 def eco_featured? object.get_field_value('ecommerce_featured').to_s.to_bool end |
#get_default_variation ⇒ Object
return the first variation of a product
149 150 151 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 149 def get_default_variation @_cama_cache_get_default_variation ||= object.product_variations.first end |
#get_variation(variation_id) ⇒ Object
return a product variation by id
74 75 76 77 78 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 74 def get_variation(variation_id) object.cama_fetch_cache("_get_variation_#{variation_id}") do self.product_variations.find(variation_id) end end |
#in_stock?(variation_id = nil) ⇒ Boolean
52 53 54 55 56 57 58 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 52 def in_stock?(variation_id = nil) if variation_id.present? get_variation(variation_id).qty > 0 else object.get_field_value('ecommerce_stock').to_s.to_bool end end |
#is_variation_product? ⇒ Boolean
check if current product is a variation product
134 135 136 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 134 def is_variation_product? @_cache_is_variation_product ||= self.product_variations.any? end |
#map_variations_the_qty_real ⇒ Object
return (Hash) all variations qty for each variation, sample: 10, 5: 2, 3: 0
139 140 141 142 143 144 145 146 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 139 def map_variations_the_qty_real res = {} return res unless is_variation_product? object.product_variations.eager_load(:product).each do |var| res[var.id] = var.product.decorate.the_qty_real end res end |
#price(variation_id = nil) ⇒ Object
60 61 62 63 64 65 66 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 60 def price(variation_id = nil) if variation_id.present? get_variation(variation_id).amount || 0 else is_variation_product? ? (get_default_variation.amount || 0) : object.get_field_value(:ecommerce_price).to_f || 0 end end |
#sku(variation_id = nil) ⇒ Object
7 8 9 10 11 12 13 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 7 def sku(variation_id = nil) if variation_id.present? get_variation(variation_id).sku else is_variation_product? ? get_default_variation.sku : object.get_field_value('ecommerce_sku').to_s end end |
#tax(variation_id = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 84 def tax(variation_id = nil) tax_rate_id = object.get_field_value(:ecommerce_tax) if tax_rate_id.present? percent = h.current_site.tax_rates.find(tax_rate_id).[:rate].to_f rescue 0 price(variation_id) * percent / 100 else # tax not defined 0 end end |
#the_featured_status ⇒ Object
106 107 108 109 110 111 112 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 106 def the_featured_status if eco_featured? "<span class='label label-primary'>#{I18n.t('plugins.ecommerce.product.featured')}</span>" else "" end end |
#the_photos ⇒ Object
48 49 50 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 48 def the_photos object.get_field_values('ecommerce_photos') || [] end |
#the_price(variation_id = nil) ⇒ Object
15 16 17 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 15 def the_price(variation_id = nil) h.e_parse_price(price(variation_id)) end |
#the_qty(variation_id = nil) ⇒ Object
31 32 33 34 35 36 37 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 31 def the_qty(variation_id = nil) if variation_id.present? get_variation(variation_id).qty || 0 else is_variation_product? ? (get_default_variation.qty || 0) : object.get_field_value('ecommerce_qty').to_i || 0 end end |
#the_qty_real(variation_id = nil) ⇒ Object
return the total (Integer) of products available to sell (doesn’t include the qty of the current cart)
40 41 42 43 44 45 46 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 40 def the_qty_real(variation_id = nil) return the_qty_real(get_default_variation.id) if !variation_id.present? && is_variation_product? carts = h.current_site.carts.active_cart.joins(:product_items) _q = variation_id.present? ? {variation_id: variation_id} : {product_id: object.id} _qty_in_carts = carts.where.not(id: h.e_current_cart.id).where("#{Plugins::Ecommerce::ProductItem.table_name}" => _q).sum("#{Plugins::Ecommerce::ProductItem.table_name}.qty") the_qty - _qty_in_carts end |
#the_sku(variation_id = nil) ⇒ Object
3 4 5 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 3 def the_sku(variation_id = nil) sku(variation_id) end |
#the_stock_status(variation_id = nil) ⇒ Object
94 95 96 97 98 99 100 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 94 def the_stock_status(variation_id = nil) if in_stock?(variation_id) && the_qty_real.to_i > 0 "<span class='label label-success'>#{I18n.t('plugins.ecommerce.product.in_stock')}</span>" else "<span class='label label-danger'>#{I18n.t('plugins.ecommerce.product.not_in_tock')}</span>" end end |
#the_tax(variation_id = nil) ⇒ Object
80 81 82 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 80 def the_tax(variation_id = nil) h.e_parse_price(tax(variation_id)) end |
#the_variation_title(variation_id = nil) ⇒ Object
return the title for variation prefixed with the title of the product
69 70 71 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 69 def the_variation_title(variation_id = nil) "#{the_title}#{" - #{get_variation(variation_id).attribute_values.pluck(:label).join(', ').translate.presence || 'Not defined'}" if variation_id.present? }" end |
#the_weight(variation_id = nil) ⇒ Object
19 20 21 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 19 def the_weight(variation_id = nil) "#{h.current_site.current_weight} #{weight(variation_id)}" end |
#valid_variation?(variation_id = nil) ⇒ Boolean
verify if current product needs a variation id to be purchased return true/false
125 126 127 128 129 130 131 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 125 def valid_variation?(variation_id = nil) if self.product_variations.any? self.product_variations.where(id: variation_id).any? else true end end |
#weight(variation_id = nil) ⇒ Object
23 24 25 26 27 28 29 |
# File 'app/decorators/plugins/ecommerce/product_decorator.rb', line 23 def weight(variation_id = nil) if variation_id.present? get_variation(variation_id).weight || 0 else is_variation_product? ? (get_default_variation.weight || 0) : object.get_field_value('ecommerce_weight').to_f || 0 end end |