Class: Spree::Api::V3::Store::CartsController
- Inherits:
-
Store::ResourceController
- Object
- Store::ResourceController
- Spree::Api::V3::Store::CartsController
- Includes:
- CartResolvable, OrderLock
- Defined in:
- app/controllers/spree/api/v3/store/carts_controller.rb
Instance Method Summary collapse
-
#associate ⇒ Object
PATCH /api/v3/store/carts/:id/associate Associates a guest cart with the currently authenticated user.
-
#complete ⇒ Object
POST /api/v3/store/carts/:id/complete Completes the checkout — returns Order (not Cart).
-
#create ⇒ Object
POST /api/v3/store/carts Creates a new shopping cart (order) Can be created by guests or authenticated customers.
-
#destroy ⇒ Object
DELETE /api/v3/store/carts/:id Deletes/abandons the cart.
-
#show ⇒ Object
GET /api/v3/store/carts/:id Returns cart by prefixed ID Auto-advances the checkout state machine so that shipments and payment requirements are up-to-date (temporary until Spree 6 removes the state machine).
-
#update ⇒ Object
PATCH /api/v3/store/carts/:id Updates cart info (email, addresses, special instructions).
Instance Method Details
#associate ⇒ Object
PATCH /api/v3/store/carts/:id/associate Associates a guest cart with the currently authenticated user. Requires: JWT authentication + cart ID in URL + possession of the cart's token (x-spree-token). The token is the credential that authorizes claiming the cart, so it is required even when the cart is already the caller's own.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 91 def associate @cart = find_cart_for_association (:update, @cart, cart_token) require_cart_token! result = Spree.cart_associate_service.call(guest_order: @cart, user: current_user, guest_only: true) if result.success? render_cart else render_service_error(result.error.to_s) end end |
#complete ⇒ Object
POST /api/v3/store/carts/:id/complete Completes the checkout — returns Order (not Cart). Idempotent: if the cart is already completed, falls back to the orders scope and returns the completed order.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 109 def complete find_cart! if @cart.guest_checkout_disallowed? return render_authentication_required('api.errors.guest_checkout_not_allowed', 'You must be signed in to complete checkout') end result = Spree::Dependencies.carts_complete_service.constantize.call(cart: @cart) if result.success? @cart = result.value render_order else render_service_error( result.error.to_s.presence || 'Could not complete checkout', code: ERROR_CODES[:cart_cannot_complete] ) end rescue ActiveRecord::RecordNotFound @cart = current_store.orders.complete.find_by_prefix_id!(params[:id]) (:show, @cart, cart_token) render_order end |
#create ⇒ Object
POST /api/v3/store/carts Creates a new shopping cart (order) Can be created by guests or authenticated customers
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 32 def create result = Spree::Carts::Create.call( params: permitted_params.merge( user: current_user, store: current_store, channel: current_channel, currency: current_currency, locale: current_locale ) ) if result.success? @cart = result.value render_cart(status: :created) else render_service_error(result.error.to_s) end end |
#destroy ⇒ Object
DELETE /api/v3/store/carts/:id Deletes/abandons the cart
73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 73 def destroy find_cart! result = Spree.cart_destroy_service.call(order: @cart) if result.success? head :no_content else render_service_error(result.error.to_s) end end |
#show ⇒ Object
GET /api/v3/store/carts/:id Returns cart by prefixed ID Auto-advances the checkout state machine so that shipments and payment requirements are up-to-date (temporary until Spree 6 removes the state machine).
17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 17 def show @cart = find_cart if @cart.ship_address_id.present? && @cart.shipments.empty? ActiveRecord::Base.connected_to(role: :writing) do with_order_lock { Spree::Checkout::Advance.call(order: @cart) } end end render_cart end |
#update ⇒ Object
PATCH /api/v3/store/carts/:id Updates cart info (email, addresses, special instructions). Auto-advances to the next checkout step when possible.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/controllers/spree/api/v3/store/carts_controller.rb', line 54 def update find_cart! with_order_lock do result = Spree::Carts::Update.call( cart: @cart, params: permitted_params ) if result.success? render_cart else render_service_error(result.error, code: ERROR_CODES[:validation_error]) end end end |