Class: Spree::LineItemsController

Inherits:
StoreController show all
Includes:
CartMethods
Defined in:
app/controllers/spree/line_items_controller.rb

Instance Method Summary collapse

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?

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

#createObject



13
14
15
16
17
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
# File 'app/controllers/spree/line_items_controller.rb', line 13

def create
  @order    = current_order(create_order_if_necessary: true)
  @quantity = params[:quantity].to_i || 1
  options   = params[:options] || {}

  cookies.permanent.signed[:token] = @order.token if @order.persisted?

  result = cart_add_item_service.call(order: @order,
                                      variant: @variant,
                                      quantity: @quantity,
                                      options: options)

  flash.now[:error] = result.value.errors.full_messages.to_sentence if result.failure?

  @line_item = result.value

  if result.success?
    load_line_items

    track_event('product_added', { line_item: @line_item })
  else
    @error = result.value.errors.full_messages.to_sentence
    flash.now[:error] = @error
  end

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to spree.cart_path(order_token: order_token) }
  end
end

#destroyObject



58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/spree/line_items_controller.rb', line 58

def destroy
  result = cart_remove_line_item_service.call(order: @order, line_item: @line_item)
  load_line_items if result.success?

  track_event('product_removed', { line_item: @line_item })

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to spree.cart_path(order_token: order_token) }
  end
end

#updateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/spree/line_items_controller.rb', line 44

def update
  quantity = line_item_params[:quantity]&.to_i || 1
  result = cart_set_item_quantity_service.call(order: @order, line_item: @line_item, quantity: quantity)

  @error = result.value.errors.full_messages.to_sentence if result.failure?

  load_line_items if result.success?

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to spree.cart_path(order_token: order_token) }
  end
end