Class: Spree::OrdersController

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

Instance Attribute Summary

Attributes included from Core::ControllerHelpers

#title

Instance Method Summary collapse

Methods included from Core::ControllerHelpers

#access_forbidden, included

Instance Method Details

#accurate_titlesObject



66
67
68
# File 'app/controllers/spree/orders_controller.rb', line 66

def accurate_titles
  @order && @order.completed? ? "#{Order.model_name.human} #{@order.number}" : t(:shopping_cart)
end

#editObject

Shows the current incomplete order from the session



25
26
27
# File 'app/controllers/spree/orders_controller.rb', line 25

def edit
  @order = current_order(true)
end

#emptyObject



58
59
60
61
62
63
64
# File 'app/controllers/spree/orders_controller.rb', line 58

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

  redirect_to spree.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}



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/spree/orders_controller.rb', line 39

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]

  fire_event('spree.cart.add')
  fire_event('spree.order.contents_changed')
  respond_with(@order) { |format| format.html { redirect_to cart_path } }
end

#showObject



8
9
10
11
# File 'app/controllers/spree/orders_controller.rb', line 8

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

#updateObject



13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/spree/orders_controller.rb', line 13

def update
  @order = current_order
  if @order.update_attributes(params[:order])
    @order.line_items = @order.line_items.select {|li| li.quantity > 0 }
    fire_event('spree.order.contents_changed')
    respond_with(@order) { |format| format.html { redirect_to cart_path } }
  else
    respond_with(@order)
  end
end