Module: Shoppy::CartHelper
- Defined in:
- app/helpers/shoppy/cart_helper.rb
Class Method Summary collapse
- .add_variant_to_customer_cart(customer, variant, quantity) ⇒ Object
- .convert_cart_to_order(customer) ⇒ Object
- .empty_cart_for_customer(customer) ⇒ Object
- .remove_variant_from_customer_cart(customer, variant) ⇒ Object
- .update_variant_quantity_in_customer_cart(customer, variant, quantity) ⇒ Object
Class Method Details
.add_variant_to_customer_cart(customer, variant, quantity) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'app/helpers/shoppy/cart_helper.rb', line 13 def self.add_variant_to_customer_cart(customer, variant, quantity) ci = CartItem.find_by(customer_id: customer.id, variant_id: variant.od) if !ci ci = CartItem.new ci.customer = customer ci.variant = variant end ci.quantity += quantity return ci.save end |
.convert_cart_to_order(customer) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/helpers/shoppy/cart_helper.rb', line 38 def self.convert_cart_to_order(customer) uc = customer.cart_items order = nil Order.transaction do o = Order.new o.customer = customer o.save order_total_price = 0.0 uc.each do |cl| ol = OrderLine.new ol.order = o ol.variant = cl.variant ol.quantity = cl.quantity ol.price_per_one = cl.variant.price line_totalprice = ol.quantity * ol.price_per_one order_total_price += line_totalprice ol.save end o.total_price = order_total_price o.date = Date.today order = o.save CartHelper::empty_cart_for_customer(customer) OrdersHelper::calculate_points(o) return true end end |
.empty_cart_for_customer(customer) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'app/helpers/shoppy/cart_helper.rb', line 3 def self.empty_cart_for_customer(customer) CartItem.transaction do cis = CartItem.where(customer_id: customer.id) cis.each do |ci| ci.destroy end return true end end |
.remove_variant_from_customer_cart(customer, variant) ⇒ Object
24 25 26 27 |
# File 'app/helpers/shoppy/cart_helper.rb', line 24 def self.remove_variant_from_customer_cart(customer, variant) ci = CartItem.find_by(customer_id: customer.id, variant_id: variant.od) ci.destroy if ci end |
.update_variant_quantity_in_customer_cart(customer, variant, quantity) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'app/helpers/shoppy/cart_helper.rb', line 29 def self.update_variant_quantity_in_customer_cart(customer, variant, quantity) ci = CartItem.find_by(customer_id: customer.id, variant_id: variant.od) if ci ci.quantity = quantity ci.save end return ci end |