Class: Spree::CheckoutController

Inherits:
StoreController show all
Includes:
BaseHelper, CheckoutAnalyticsHelper, CheckoutHelper
Defined in:
app/controllers/spree/checkout_controller.rb

Instance Method Summary collapse

Methods included from CheckoutAnalyticsHelper

#clean_analytics_session, #clear_checkout_completed_session, #coupon_tracking_params, #order_tracking_params, #track_checkout_completed, #track_checkout_entered_email, #track_checkout_started, #track_checkout_step_completed, #track_checkout_step_viewed, #track_payment_info_entered

Methods included from CheckoutHelper

#already_have_an_account?, #can_use_store_credit_on_checkout?, #checkout_available_payment_methods, #checkout_payment_sources, #checkout_progress, #checkout_started?, #quick_checkout_enabled?

Methods inherited from StoreController

#current_taxon, #default_products_sort, #permitted_products_params, #products_filters_params, #render_404_if_store_not_exists, #store_filter_names, #store_filter_names_hash

Methods included from AnalyticsHelper

#analytics_event_handlers, #track_event, #unsupported_event?, #visitor_id

Methods included from WishlistHelper

#current_wishlist

Methods included from PasswordProtected

#redirect_to_password

Methods included from StorefrontHelper

#as_aspect_ratio, #page_description, #page_image, #paths_equal?, #render_storefront_partials, #show_account_pane?, #svg_country_icon, #tailwind_classes_for

Methods included from ThemeConcern

#default_url_options, #set_theme_view_paths

Instance Method Details

#apply_coupon_codeObject

PATCH /checkout/<token>/coupon_code



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/spree/checkout_controller.rb', line 91

def apply_coupon_code
  @order.coupon_code = params[:coupon_code]
  @result = coupon_handler.new(@order, { email: params[:promotion_email] }).apply

  track_event('coupon_entered', coupon_tracking_params)

  if @result&.successful?
    track_event('coupon_applied', coupon_tracking_params)
  else
    track_event('coupon_denied', coupon_tracking_params)
  end

  respond_to do |format|
    format.turbo_stream
    format.html do
      if @result&.successful?
        flash[:success] = 'Coupon applied successfully'
      else
        flash[:error] = @result&.error
      end
      redirect_to spree.checkout_path(@order.token)
    end
  end
end

#apply_store_creditObject



129
130
131
132
133
134
135
136
# File 'app/controllers/spree/checkout_controller.rb', line 129

def apply_store_credit
  add_store_credit_service.call(order: @order)

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to spree.checkout_path(@order.token) }
  end
end

#completeObject

GET /checkout/<token>/complete



86
87
88
# File 'app/controllers/spree/checkout_controller.rb', line 86

def complete
  clear_order_token
end

#editObject

GET /checkout/<token>



36
37
38
# File 'app/controllers/spree/checkout_controller.rb', line 36

def edit
  track_checkout_step_viewed
end

#remove_coupon_codeObject

DELETE /checkout/<token>/coupon_code



117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/spree/checkout_controller.rb', line 117

def remove_coupon_code
  @result = coupon_handler.new(@order).remove(params[:coupon_code] || params[:gift_card])

  track_event('coupon_removed', coupon_tracking_params) if @result.successful?
  params.delete(:coupon_code)

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to spree.checkout_path(@order.token) }
  end
end

#remove_missing_itemsObject

DELETE /checkout/<token>/remove_missing_items



73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/spree/checkout_controller.rb', line 73

def remove_missing_items
  line_items_to_remove = @order.line_items.where(id: params[:line_item_ids])

  ApplicationRecord.transaction do
    line_items_to_remove.each do |line_item|
      remove_line_item_service.call(order: @order, line_item: line_item)
    end
  end

  redirect_to spree.checkout_path(@order.token)
end

#remove_store_creditObject



138
139
140
141
142
143
144
145
# File 'app/controllers/spree/checkout_controller.rb', line 138

def remove_store_credit
  remove_store_credit_service.call(order: @order)

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to spree.checkout_path(@order.token) }
  end
end

#updateObject

PATCH /checkout/<token> Updates the order and advances to the next state (when possible.) Passing params = true only updates order without the need to advance to the next state.



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
# File 'app/controllers/spree/checkout_controller.rb', line 43

def update
  @previous_state = @order.state

  if @order.update_from_params(params, permitted_checkout_attributes, request.headers.env)
    track_checkout_entered_email
    track_payment_info_entered
    track_checkout_step_completed

    unless params[:do_not_advance]
      @order.temporary_address = !params[:save_user_address]
      unless @order.next
        return if @order.address? && @order.line_items_without_shipping_rates.any? && turbo_stream_request? # render update trubo_stream

        flash[:error] = @order.errors.messages.values.flatten.join("\n")
        redirect_to(spree.checkout_state_path(@order.token, @order.state)) && return
      end

      if @order.completed?
        track_checkout_completed
        redirect_to spree.checkout_complete_path(@order.token), status: :see_other
      else
        redirect_to spree.checkout_state_path(@order.token, @order.state)
      end
    end
  else
    render :edit, status: :unprocessable_entity
  end
end