Module: Spree::Core::ControllerHelpers::Order

Extended by:
ActiveSupport::Concern
Defined in:
lib/spree/core/controller_helpers/order.rb

Instance Method Summary collapse

Instance Method Details

#associate_userObject



46
47
48
49
50
51
# File 'lib/spree/core/controller_helpers/order.rb', line 46

def associate_user
  @order ||= current_order
  if try_spree_current_user && @order
    @order.associate_user!(try_spree_current_user) if @order.user.blank? || @order.email.blank?
  end
end

#current_order(options = {}) ⇒ Object

The current incomplete order from the token for use in cart and during checkout



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spree/core/controller_helpers/order.rb', line 18

def current_order(options = {})
  options[:create_order_if_necessary] ||= false
  options[:includes] ||= false

  if @current_order
    @current_order.last_ip_address = ip_address
    return @current_order
  end

  @current_order = find_order_by_token_or_user(options, false)

  if options[:create_order_if_necessary] && (@current_order.nil? || @current_order.completed?)
    @current_order = current_store.orders.create!(current_order_params.except(:token))
    @current_order.associate_user! try_spree_current_user if try_spree_current_user
    @current_order.last_ip_address = ip_address

    create_token_cookie(@current_order.token)
  end

  # There is some edge case where the order doesn't have a token.
  # but can't reproduce it. So let's generate one on the fly in that case.
  @current_order.regenerate_token if @current_order && @current_order.token.blank?

  create_token_cookie(@current_order&.token || current_order_params[:token] || params[:token]) if create_cookie_from_token?

  @current_order
end

#ip_addressObject



78
79
80
# File 'lib/spree/core/controller_helpers/order.rb', line 78

def ip_address
  request.remote_ip
end

#order_tokenObject



13
14
15
# File 'lib/spree/core/controller_helpers/order.rb', line 13

def order_token
  @order_token ||= cookies.signed[:token] || params[:order_token]
end

#set_current_orderObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/spree/core/controller_helpers/order.rb', line 53

def set_current_order
  return unless try_spree_current_user && current_order

  orders_scope = user_orders_scope

  orders_to_merge = orders_scope.limit(10)
  order_ids_to_delete = orders_scope.ids - orders_to_merge.ids

  orders_scope_exists = orders_scope.exists?

  if orders_scope.exists?
    ActiveRecord::Base.connected_to(role: :writing) do
      orders_to_merge.find_each do |order|
        current_order.merge!(order, try_spree_current_user)
      end

      Spree::Order.where(id: order_ids_to_delete).find_each do |order|
        Rails.logger.error("Failed to destroy order #{order.id} while merging") unless order.destroy
      end
    end
  end

  orders_scope_exists
end