2023-10-26 02:37:13 +00:00
# Checkout Store (`wc/store/checkout`) <!-- omit in toc -->
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
> 💡 What's the difference between the Cart Store and the Checkout Store?
>
> The **Cart Store (`wc/store/cart`)** manages and retrieves data about the shopping cart, including items, customer data, and interactions like coupons.
>
> The **Checkout Store (`wc/store/checkout`)** manages and retrieves data related to the checkout process, customer IDs, order IDs, and checkout status.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
## Table of Contents <!-- omit in toc -->
- [Overview ](#overview )
- [Usage ](#usage )
2022-09-23 04:33:08 +00:00
- [Selectors ](#selectors )
- [getCustomerId ](#getcustomerid )
2022-11-24 14:19:59 +00:00
- [getOrderId ](#getorderid )
2022-09-23 04:33:08 +00:00
- [getOrderNotes ](#getordernotes )
2022-11-24 14:19:59 +00:00
- [getRedirectUrl ](#getredirecturl )
- [getExtensionData ](#getextensiondata )
- [getCheckoutStatus ](#getcheckoutstatus )
- [getShouldCreateAccount ](#getshouldcreateaccount )
- [getUseShippingAsBilling ](#getuseshippingasbilling )
2022-09-23 04:33:08 +00:00
- [hasError ](#haserror )
- [hasOrder ](#hasorder )
- [isIdle ](#isidle )
2022-11-24 14:19:59 +00:00
- [isBeforeProcessing ](#isbeforeprocessing )
2022-09-23 04:33:08 +00:00
- [isProcessing ](#isprocessing )
2022-11-24 14:19:59 +00:00
- [isAfterProcessing ](#isafterprocessing )
- [isComplete ](#iscomplete )
- [isCalculating ](#iscalculating )
2023-12-08 15:15:34 +00:00
- [prefersCollection ](#preferscollection )
2023-12-06 08:58:25 +00:00
- [Actions ](#actions )
- [setPrefersCollection ](#setpreferscollection )
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
## Overview
The Checkout Store provides a collection of selectors to access and manage data during the checkout process. These selectors enable developers to fetch key details such as customer information, order status, and other checkout-related data.
## Usage
To utilize this store you will import the `CHECKOUT_STORE_KEY` in any module referencing it. Assuming `@woocommerce/block-data` is registered as an external pointing to `wc.wcBlocksData` you can import the key via:
```js
2023-11-03 16:30:19 +00:00
const { CHECKOUT_STORE_KEY } = window.wc.wcBlocksData;
2023-10-26 02:37:13 +00:00
```
2022-09-23 04:33:08 +00:00
## Selectors
### getCustomerId
Returns the WordPress user ID of the customer whose order is currently processed by the Checkout block.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
- `number` : The WordPress user ID of the customer.
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const customerId = store.getCustomerId();
```
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
### getOrderId
Returns the WooCommerce order ID of the order that is currently being processed by the Checkout block.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `number` : The WooCommerce order ID.
2022-11-24 14:19:59 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const orderId = store.getOrderId();
```
2022-11-24 14:19:59 +00:00
2022-09-23 04:33:08 +00:00
### getOrderNotes
Returns the order notes.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `string` : The order notes.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const orderNotes = store.getOrderNotes();
```
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
### getRedirectUrl
Returns the URL to redirect to after checkout is complete.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `string` : The URL to redirect to.
#### _Example_ <!-- omit in toc -->
2022-11-24 14:19:59 +00:00
2023-10-26 02:37:13 +00:00
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const redirectUrl = store.getRedirectUrl();
```
2022-11-24 14:19:59 +00:00
### getExtensionData
Returns the extra data registered by extensions.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-24 14:19:59 +00:00
2023-10-26 02:37:13 +00:00
- `object` : The extra data registered by extensions.
2022-11-24 14:19:59 +00:00
```js
{
[ extensionNamespace ]: {
[ key ]: value,
},
}
```
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const extensionData = store.getExtensionData();
```
2022-11-24 14:19:59 +00:00
### getCheckoutStatus
Returns the current status of the checkout process.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-24 14:19:59 +00:00
2023-10-26 02:37:13 +00:00
- `string` : The current status of the checkout process. Possible values are: `pristine` , `before-processing` , `processing` , `after-processing` , `complete` , `idle` .
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const checkoutStatus = store.getCheckoutStatus();
```
2022-11-24 14:19:59 +00:00
### getShouldCreateAccount
Returns true if the shopper has opted to create an account with their order.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-24 14:19:59 +00:00
- `boolean` : True if the shopper has opted to create an account with their order.
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const shouldCreateAccount = store.getShouldCreateAccount();
```
2022-11-24 14:19:59 +00:00
### getUseShippingAsBilling
Returns true if the shopper has opted to use their shipping address as their billing address.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-11-24 14:19:59 +00:00
- `boolean` : True if the shipping address should be used as the billing address.
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const useShippingAsBilling = store.getUseShippingAsBilling();
```
2022-09-23 04:33:08 +00:00
### hasError
Returns true if an error occurred, and false otherwise.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if an error occurred.
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const hasError = store.hasError();
```
2022-09-23 04:33:08 +00:00
### hasOrder
Returns true if a draft order had been created, and false otherwise.
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if a draft order had been created.
#### _Example_ <!-- omit in toc -->
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const hasOrder = store.hasOrder();
```
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
### isIdle
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
When the checkout status is `IDLE` this flag is true. Checkout will be this status after any change to checkout state after the block is loaded. It will also be this status when retrying a purchase is possible after processing happens with an error.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if the checkout has had some activity, but is currently waiting for user input.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const isIdle = store.isIdle();
```
2022-09-23 04:33:08 +00:00
### isBeforeProcessing
2022-11-24 14:19:59 +00:00
When the checkout status is `BEFORE_PROCESSING` this flag is true. Checkout will be this status when the user submits checkout for processing.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if an order is about to be processed.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const isBeforeProcessing = store.isBeforeProcessing();
```
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
### isProcessing
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
When the checkout status is `PROCESSING` this flag is true. Checkout will be this status when all the observers on the event emitted with the `BEFORE_PROCESSING` status are completed without error. It is during this status that the block will be sending a request to the server on the checkout endpoint for processing the order.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if the checkout is processing.
#### _Example_ <!-- omit in toc -->
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const isProcessing = store.isProcessing();
```
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
### isAfterProcessing
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
When the checkout status is `AFTER_PROCESSING` this flag is true. Checkout will have this status after the the block receives the response from the server side processing request.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if an order had just been processed.
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const isAfterProcessing = store.isAfterProcessing();
```
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
### isComplete
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
When the checkout status is `COMPLETE` this flag is true. Checkout will have this status after all observers on the events emitted during the `AFTER_PROCESSING` status are completed successfully. When checkout is at this status, the shopper's browser will be redirected to the value of `redirectUrl` at that point (usually the `order-received` route).
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
- `boolean` : True if the order is complete.
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const isComplete = store.isComplete();
```
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
### isCalculating
2022-09-23 04:33:08 +00:00
2022-11-24 14:19:59 +00:00
This is true when the total is being re-calculated for the order. There are numerous things that might trigger a recalculation of the total: coupons being added or removed, shipping rates updated, shipping rate selected etc. This flag consolidates all activity that might be occurring (including requests to the server that potentially affect calculation of totals). So instead of having to check each of those individual states you can reliably just check if this boolean is true (calculating) or false (not calculating).
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Returns_ <!-- omit in toc -->
- `boolean` : True if there is an in-flight request to update any values.
2022-09-23 04:33:08 +00:00
2023-10-26 02:37:13 +00:00
#### _Example_ <!-- omit in toc -->
```js
2023-12-06 08:58:25 +00:00
const store = select( CHECKOUT_STORE_KEY );
2023-10-26 02:37:13 +00:00
const isCalculating = store.isCalculating();
```
2022-09-23 04:33:08 +00:00
2023-12-08 15:15:34 +00:00
### prefersCollection
Returns true if the customer prefers to collect their order, and false otherwise.
#### _Returns_ <!-- omit in toc -->
- _prefersCollection_ `boolean` : True if the shopper prefers to collect their order.
#### _Example_ <!-- omit in toc -->
```js
const store = select( CHECKOUT_STORE_KEY );
const prefersCollection = store.prefersCollection();
```
2023-12-06 08:58:25 +00:00
## Actions
### setPrefersCollection
Sets the `prefersCollection` flag to true or false.
#### _Parameters_ <!-- omit in toc -->
- _prefersCollection_ `boolean` : True if the shopper prefers to collect their order.
#### _Example_ <!-- omit in toc -->
```js
const store = dispatch( CHECKOUT_STORE_KEY );
store.setPrefersCollection( true );
```
2022-09-23 04:33:08 +00:00
<!-- FEEDBACK -->
---
2024-04-09 08:50:15 +00:00
[We're hiring! ](https://woocommerce.com/careers/ ) Come work with us!
2022-09-23 04:33:08 +00:00
2022-11-10 08:52:04 +00:00
🐞 Found a mistake, or have a suggestion? [Leave feedback about this document here. ](https://github.com/woocommerce/woocommerce-blocks/issues/new?assignees=&labels=type%3A+documentation&template=--doc-feedback.md&title=Feedback%20on%20./docs/third-party-developers/extensibility/data-store/checkout.md )
2022-09-23 04:33:08 +00:00
<!-- /FEEDBACK -->