Class: OrdersController

Inherits:
Spree::BaseController show all
Defined in:
app/controllers/orders_controller.rb

Instance Method Summary collapse

Methods included from SpreeBase

included

Instance Method Details

#accurate_titleObject



58
59
60
# File 'app/controllers/orders_controller.rb', line 58

def accurate_title
  I18n.t(:shopping_cart)
end

#editObject

Shows the current incomplete order from the session



20
21
22
# File 'app/controllers/orders_controller.rb', line 20

def edit
  @order = current_order(true)
end

#emptyObject



51
52
53
54
55
56
# File 'app/controllers/orders_controller.rb', line 51

def empty
  if @order = current_order
    @order.line_items.destroy_all
  end
  redirect_to cart_path
end

#populateObject

Adds a new item to the order (creating a new order if none already exists)

Parameters can be passed using the following possible parameter configurations:

  • Single variant/quantity pairing

:variants => {variant_id => quantity}

  • Multiple products at once

:products => {product_id => variant_id, product_id => variant_id}, :quantity => quantity :products => {product_id => variant_id, product_id => variant_id}}, :quantity => {variant_id => quantity, variant_id => quantity}



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/orders_controller.rb', line 34

def populate
  @order = current_order(true)

  params[:products].each do |product_id,variant_id|
    quantity = params[:quantity].to_i if !params[:quantity].is_a?(Hash)
    quantity = params[:quantity][variant_id].to_i if params[:quantity].is_a?(Hash)
    @order.add_variant(Variant.find(variant_id), quantity) if quantity > 0
  end if params[:products]

  params[:variants].each do |variant_id, quantity|
    quantity = quantity.to_i
    @order.add_variant(Variant.find(variant_id), quantity) if quantity > 0
  end if params[:variants]

  redirect_to cart_path
end

#showObject



5
6
7
# File 'app/controllers/orders_controller.rb', line 5

def show
  @order = Order.find_by_number(params[:id])
end

#updateObject



9
10
11
12
13
14
15
16
17
# File 'app/controllers/orders_controller.rb', line 9

def update
  @order = current_order
  if @order.update_attributes(params[:order])
    @order.line_items = @order.line_items.select {|li| li.quantity > 0 }
    redirect_to cart_path
  else
    render :edit
  end
end