Simple REST checkout flow
This topic is subject to change as the progress of Magento 2.3 GraphQL coverage continues to expand.
The checkout process is an important piece of any online store. It can be as simple as a single click or as complicated as filling out a series of forms to complete a purchase.
The goal of this topic is to describe a simple, end-to-end checkout implementation in Venia to learn from and build upon in your own projects.
Magento REST endpoints
GraphQL is a significant improvement over REST when working with asynchronous data requests. In its current state, Magento 2.3 GraphQL coverage only includes a limited subset of catalog operations, so the checkout flow described in this topic uses the REST API to complete the process.
The following is a list of Magento REST endpoints and HTTP methods used for this checkout flow:
| REST Endpoint | HTTP method | Usage |
|---|---|---|
/V1/guest-carts |
POST | Get information about or create and store a new cart ID |
/V1/guest-carts/{cartId}/items |
POST | Update cart with item details |
/V1/guest-carts/{cartId} |
GET | Get information about the cart items |
/V1/guest-carts/{cartId}/totals |
GET | Get information about cart totals |
For a list of Magento REST endpoints, see the REST API documentation in devdocs.
Shopper experience
Before going into the technical steps that happens in the background, you should be familiar with the events surrounding the checkout process.
The following steps summarize the basic checkout experience for an online shopper in Venia:
- The shopper navigates to a product page.
- The shopper adds an item to the cart using the Add to Cart button.
- The shopping cart drawer slides out as a response to the add item action.
- The shopper clicks on the Checkout button.
- The Shipping and Billing form appears and replaces the shopping cart drawer.
- The shopper fills out the form or uses preset account information and clicks on the Submit Order button.
- A confirmation page appears and the shopper clicks on Continue Shopping to go back to the product page.
Detailed technical flow
The following sections provide the technical details for each step in the checkout flow.
Updating the cart
- When the shopper clicks on the Add To Cart button, the Product component calls the
addItemToCartaction creator and waits for the action resolution. -
The
addItemToCartfunction checks the local storage for an existing cart ID.If an existing cart ID does not exist, the function dispatches a
CREATE_GUEST_CARTaction, using data from a POST request to the/V1/guest-cartsREST endpoint, to assign a cart ID to the current session.After the function obtains an existing cart ID, it dispatches an
ADD_ITEM_TO_CARTaction, using data from a POST request toV1/guest-carts/<cartID>/items, to update the cart. This request also updates the cart information stored on the server. -
When the
ADD_ITEM_TO_CARTreturns with a successful response, the function callsgetCartDetailsandtoggleCart.The
getCartDetailsaction creator makes two simultaneous calls to the/V1/guest-carts/<cartId>and/V1/guest-carts/<cartId>/totalsREST endpoints. When the parallel requests return with successful responses, it combines the data to create a payload for theGET_CART_DETAILSaction. This action updates the information displayed on the cart with the new data.The
toggleCartaction creator dispatches theTOGGLE_DRAWERaction to open the cart for viewing.
Checking out
- When the shopper clicks on the Checkout button, the
requestOrderaction creator dispatches theREQUEST_ORDERaction. - After the action completes, the function dispatches the
RECEIVE_ORDERaction.
Submitting the order
- When the shopper clicks on the Submit Order button, the
submitOrderaction creator dispatches theSUBMIT_ORDERaction. - After the action completes, the function dispatches the
ACCEPT_ORDERaction.
Completing the checkout flow
The checkout flow ends with the shopper clicking on the Continue Shopping button.
This calls the resetCheckout action creator which calls closeDrawer to hide the cart drawer and dispatches the RESET_CHECKOUT action.
The RESET_CHECKOUT action returns the cart to its default empty state.