Class: Spree::OrdersController

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

Instance Method Summary collapse

Methods inherited from StoreController

#api_tokens, #cart_link, #ensure_cart, #forbidden, #unauthorized

Instance Method Details

#editObject

Shows the current incomplete order from the session



34
35
36
37
38
39
# File 'app/controllers/spree/orders_controller.rb', line 34

def edit
  @order = current_order || Order.incomplete.
           includes(line_items: [variant: [:images, :option_values, :product]]).
           find_or_initialize_by(token: cookies.signed[:token])
  associate_user
end

#emptyObject



95
96
97
98
99
# File 'app/controllers/spree/orders_controller.rb', line 95

def empty
  current_order.try(:empty!)

  redirect_to spree.cart_path
end

#populateObject

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/spree/orders_controller.rb', line 42

def populate
  ActiveSupport::Deprecation.warn("    OrdersController#populate is deprecated and will be removed in Spree 4.0.\n    Please use `/api/v2/storefront/cart/add_item` endpoint instead.\n    See documentation: https://github.com/spree/spree/blob/master/api/docs/v2/storefront/index.yaml#L42\n  DEPRECATION\n\n  order    = current_order(create_order_if_necessary: true)\n  variant  = Spree::Variant.find(params[:variant_id])\n  quantity = params[:quantity].to_i\n  options  = params[:options] || {}\n\n  # 2,147,483,647 is crazy. See issue #2695.\n  if quantity.between?(1, 2_147_483_647)\n    begin\n      result = cart_add_item_service.call(order: order,\n                                          variant: variant,\n                                          quantity: quantity,\n                                          options: options)\n      if result.failure?\n        error = result.value.errors.full_messages.join(', ')\n      else\n        order.update_line_item_prices!\n        order.create_tax_charge!\n        order.update_with_updater!\n      end\n    rescue ActiveRecord::RecordInvalid => e\n      error = e.record.errors.full_messages.join(', ')\n    end\n  else\n    error = Spree.t(:please_enter_reasonable_quantity)\n  end\n\n  if error\n    flash[:error] = error\n    redirect_back_or_default(spree.root_path)\n  else\n    respond_with(order) do |format|\n      format.html { redirect_to(cart_path(variant_id: variant.id)) }\n    end\n  end\nend\n", caller)

#populate_redirectObject



85
86
87
88
89
90
91
92
93
# File 'app/controllers/spree/orders_controller.rb', line 85

def populate_redirect
  ActiveSupport::Deprecation.warn("    OrdersController#populate is deprecated and will be removed in Spree 4.0.\n    Please use `/api/v2/storefront/cart/add_item` endpoint instead.\n    See documentation: https://github.com/spree/spree/blob/master/api/docs/v2/storefront/index.yaml#L42\n  DEPRECATION\n  flash[:error] = Spree.t(:populate_get_error)\n  redirect_to cart_path\nend\n", caller)

#showObject



11
12
13
# File 'app/controllers/spree/orders_controller.rb', line 11

def show
  @order = Order.includes(line_items: [variant: [:option_values, :images, :product]], bill_address: :state, ship_address: :state).find_by!(number: params[:id])
end

#updateObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/spree/orders_controller.rb', line 15

def update
  @variant = Spree::Variant.find(params[:variant_id]) if params[:variant_id]
  if Cart::Update.call(order: @order, params: order_params).success?
    respond_with(@order) do |format|
      format.html do
        if params.key?(:checkout)
          @order.next if @order.cart?
          redirect_to checkout_state_path(@order.checkout_steps.first)
        else
          redirect_to cart_path
        end
      end
    end
  else
    respond_with(@order)
  end
end