The documentation for WooCommerce Blocks has moved to the WooCommerce monorepo.
Please refer to the documentation in the new location as the files in this repository will no longer be updated and the repository will be archived.
💡 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.
- Overview
- Usage
- Actions
- setCartData
- setErrorData
- receiveCartContents
- receiveApplyingCoupon
- receiveRemovingCoupon
- receiveCartItem
- itemIsPendingQuantity
- itemIsPendingDelete
- setIsCartDataStale
- updatingCustomerData
- shippingRatesBeingSelected
- applyExtensionCartUpdate
- applyCoupon
- removeCoupon
- addItemToCart
- removeItemFromCart
- changeCartItemQuantity
- selectShippingRate
- setBillingAddress
- setShippingAddress
- updateCustomerData
- Selectors
- getCartData
- getCustomerData
- getShippingRates
- getNeedsShipping
- getHasCalculatedShipping
- getCartTotals
- getCartMeta
- getCartErrors
- isApplyingCoupon
- isCartDataStale
- getCouponBeingApplied
- isRemovingCoupon
- getCouponBeingRemoved
- getCartItem( cartItemKey )
- isItemPendingQuantity( cartItemKey )
- isItemPendingDelete( cartItemKey )
- isCustomerDataUpdating
- isShippingRateBeingSelected
- getItemsPendingQuantityUpdate
- getItemsPendingDelete
The Cart Store provides a collection of selectors and methods to manage and retrieve cart-related data for WooCommerce Blocks. It offers functionality ranging from fetching cart details to managing customer interactions, such as applying coupons or updating shipping information.
To utilize this store you will import the CART_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:
const { CART_STORE_KEY } = window.wc.wcBlocksData;
This action is used to set the cart data in the store.
- cartData
object
: The current cart data with the following keys:- coupons
array
: The coupon items in the cart. - shippingRates
array
: The cart shipping rates (seegetShippingRates
selector). - shippingAddress
object
: The shipping address (seegetCustomerData
selector). - billingAddress
object
: The billing address (seegetCustomerData
selector). - items
array
: The cart items. - itemsCount
number
: The total number of items in the cart - itemsWeight
number
: The total weight of items in the cart. - crossSells
array
: The cross sells items. - needsPayment
boolean
: If the cart needs payment. - needsShipping
boolean
: If the cart needs shipping. - hasCalculatedShipping
boolean
: If the cart has calculated shipping. - fees
array
: The cart fees. - totals
object
: The cart totals (seegetCartTotals
selector). - errors
array
: The cart errors (seegetCartErrors
selector). - paymentRequirements
object
: The payment requirements for the cart. - extensions
object
: The extensions data.
- coupons
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( setCartData( newCartData ) );
This action is used to set the error data in the store.
- errorData
object
: The error data that needs to be set in the store.- code
string
: The error code. - message
string
: The error message. - data
object
: Additional error data. This is an optional object with the following keys:- status
number
: The error status. - params
string
: The error params. - message
string
: The error message. - cart
object
: The cart data. This is an optional object with the following keys:- coupons
array
: The coupon items in the cart. - shippingRates
array
: The cart shipping rates (seegetShippingRates
selector). - shippingAddress
object
: The shipping address (seegetCustomerData
selector). - billingAddress
object
: The billing address (seegetCustomerData
selector). - items
array
: The cart items. - itemsCount
number
: The total number of items in the cart - itemsWeight
number
: The total weight of items in the cart. - crossSells
array
: The cross sells items. - needsPayment
boolean
: If the cart needs payment. - needsShipping
boolean
: If the cart needs shipping. - hasCalculatedShipping
boolean
: If the cart has calculated shipping. - fees
array
: The cart fees. - totals
object
: The cart totals (seegetCartTotals
selector). - errors
array
: The cart errors (seegetCartErrors
selector). - paymentRequirements
object
: The payment requirements for the cart. - extensions
object
: The extensions data.
- coupons
- status
- code
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( setErrorData( newErrorData ) );
This action returns an action object used in updating the store with the provided cart. It omits the customer addresses so that only updates to cart items and totals are received.
- cartContents
object
: A cart contents API response.- coupons
array
: The coupon items in the cart. - shippingRates
array
: The cart shipping rates (seegetShippingRates
selector). - shippingAddress
object
: The shipping address (seegetCustomerData
selector). - billingAddress
object
: The billing address (seegetCustomerData
selector). - items
array
: The cart items. - itemsCount
number
: The total number of items in the cart - itemsWeight
number
: The total weight of items in the cart. - crossSells
array
: The cross sells items. - needsPayment
boolean
: If the cart needs payment. - needsShipping
boolean
: If the cart needs shipping. - hasCalculatedShipping
boolean
: If the cart has calculated shipping. - fees
array
: The cart fees. - totals
object
: The cart totals (seegetCartTotals
selector). - errors
array
: The cart errors (seegetCartErrors
selector). - paymentRequirements
object
: The payment requirements for the cart. - extensions
object
: The extensions data.
- coupons
object
: The action object with the following keys:- type
string
: The action type. - cartContents
object
: The cart contents with the following keys:- coupons
array
: The coupon items in the cart. - shippingRates
array
: The cart shipping rates (seegetShippingRates
selector). - items
array
: The cart items. - itemsCount
number
: The total number of items in the cart - itemsWeight
number
: The total weight of items in the cart. - crossSells
array
: The cross sells items. - needsPayment
boolean
: If the cart needs payment. - needsShipping
boolean
: If the cart needs shipping. - hasCalculatedShipping
boolean
: If the cart has calculated shipping. - fees
array
: The cart fees. - totals
object
: The cart totals (seegetCartTotals
selector). - errors
array
: The cart errors (seegetCartErrors
selector). - paymentRequirements
object
: The payment requirements for the cart. - extensions
object
: The extensions data.
- coupons
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( receiveCartContents( newCartContents ) );
This action returns an action object used to track when a coupon is applying.
- couponCode
string
: The code of the coupon being applied.
object
: The action object with following keys:- type
string
: The action type. - couponCode
string
: The code of the coupon being applied.
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( receiveApplyingCoupon( couponCode ) );
This action returns an action object used to track when a coupon is removing.
- couponCode
string
: The code of the coupon being removed.
object
: The action object with the following keys:- type
string
: The action type. - couponCode
string
: The code of the coupon being removed.
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( receiveRemovingCoupon( couponCode ) );
This action is used to update a specific item in the cart.
- cartResponseItem
object
: Cart response object with the following keys:- cartItem
object
: The cart item (seegetCartItem
selector).
- cartItem
object
: The action object with the following keys:- type
string
: The action type. - cartItem
object
: The cart item (seegetCartItem
selector).
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( receiveCartItem( CartResponseItem ) );
This action returns an action object to indicate if the specified cart item quantity is being updated.
- cartItemKey
string
: The key of the cart item. - isPending
boolean
(default:true
): Whether the cart item quantity is being updated.
object
: The action object with following keys:- type
string
: The action type. - cartItemKey
string
: The key of the cart item. - isPending
boolean
: Whether the cart item quantity is being updated.
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( itemIsPendingQuantity( cartItemKey, isPending ) );
This action returns an action object to indicate if the specified cart item is being deleted.
- cartItemKey
string
: The key of the cart item. - isPending
boolean
(default:true
): Whether the cart item is being deleted.
object
: The action object with the following keys:- type
string
: The action type. - cartItemKey
string
: The key of the cart item. - isPending
boolean
: Whether the cart item is being deleted.
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( itemIsPendingDelete( cartItemKey, isPending ) );
This action returns an action object to indicate if the cart data is stale.
- isCartDataStale
boolean
(default:true
): Flag to mark cart data as stale; true iflastCartUpdate
timestamp is newer than the one in wcSettings.
object
: The action object with the following keys:- type
string
: The action type. - isCartDataStale
boolean
: Flag to mark cart data as stale; true iflastCartUpdate
timestamp is newer than the one in wcSettings.
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( setIsCartDataStale( isCartDataStale ) );
This action returns an action object to indicate if the customer data is being updated.
- isResolving
boolean
: Whether the customer data is being updated.
object
: The action object with the following keys:- type
string
: The action type. - isResolving
boolean
: Whether the customer data is being updated.
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( updatingCustomerData( isResolving ) );
This action returns an action object to indicate if the shipping rates are being selected.
- isResolving
boolean
: True if shipping rate is being selected.
object
: The action object with the following keys:- type
string
: The action type. - isResolving
boolean
: True if shipping rate is being selected.
- type
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( shippingRatesBeingSelected( isResolving ) );
This action is used to send POSTs request to the /cart/extensions endpoint with the data supplied by the extension.
- args
object
: The arguments for the request with the following keys:- extensionId
string
: The extension ID. - data
object
: The data to send to the endpoint with the following keys:- key
string
: The key of the extension. - value
string
: The value of the extension.
- key
- extensionId
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( applyExtensionCartUpdate( args ) );
This action is used to apply a coupon to the cart.
- couponCode
string
: The code of the coupon to apply.
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( applyCoupon( couponCode ) );
This action is used to remove a coupon from the cart.
- couponCode
string
: The code of the coupon to remove.
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( removeCoupon( couponCode ) );
This action is used to add an item to the cart.
- productId
number
: Product ID to add to cart. - quantity
number
(default:1
): The quantity of the product to add.
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( addItemToCart( productId, quantity ) );
This action is used to remove an item from the cart.
- cartItemKey
string
: Cart item being updated.
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( removeItemFromCart( cartItemKey ) );
This action is used to change the quantity of an item in the cart.
- cartItemKey
string
: Cart item being updated. - quantity
number
: Quantity of the item.
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( changeCartItemQuantity( cartItemKey, quantity ) );
This action is used to select a shipping rate for the cart.
- rateId
string
: The ID of the shipping rate to select. - packageId
number | string
(default:null
): The key of the packages that will select within the shipping rate.
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( selectShippingRate( rateId, packageId ) );
This action is used to set the billing address for the cart locally, as opposed to updateCustomerData which sends it to the server.
- billingAddress
object
: Billing address that needs to be set. The keys are as following:- first_name
string
: The first name. - last_name
string
: The last name. - company
string
: The company name. - address_1
string
: The address line 1. - address_2
string
: The address line 2. - city
string
: The city name. - state
string
: The state name. - postcode
string
: The postcode. - country
string
: The country name.
- first_name
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( setBillingAddress( billingAddress ) );
This action is used to set the shipping address for the cart locally, as opposed to updateCustomerData which sends it to the server.
- shippingAddress
object
: Shipping address that needs to be set. The keys are as following:- first_name
string
: The first name. - last_name
string
: The last name. - company
string
: The company name. - address_1
string
: The address line 1. - address_2
string
: The address line 2. - city
string
: The city name. - state
string
: The state name. - postcode
string
: The postcode. - country
string
: The country name.
- first_name
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( setShippingAddress( shippingAddress ) );
This action is used to updates the shipping and/or billing address for the customer and returns an updated cart.
- customerData
object
: Customer billing and shipping address. The keys are as following:- shippingAddress
object
: The shipping address with the following keys:- first_name
string
: The first name. - last_name
string
: The last name. - company
string
: The company name. - address_1
string
: The address line 1. - address_2
string
: The address line 2. - city
string
: The city name. - state
string
: The state name. - postcode
string
: The postcode. - country
string
: The country name.
- first_name
- billingAddress
object
: The billing address (same keys as shipping address).
- shippingAddress
editing: boolean
(default:true
): If the address is being edited, we don't update the customer data in the store from the response.
const { dispatch } = useDispatch( CART_STORE_KEY );
dispatch( updateCustomerData( customerData, editing ) );
Returns the Cart data from the state.
object
: The current cart data with the following keys:- coupons
array
: The coupon items in the cart. - shippingRates
array
: The cart shipping rates (seegetShippingRates
selector). - shippingAddress
object
: The shipping address (seegetCustomerData
selector). - billingAddress
object
: The billing address. - items
array
: The cart items. - itemsCount
number
: The total number of items in the cart - itemsWeight
number
: The total weight of items in the cart. - crossSells
array
: The cross sells items. - needsPayment
boolean
: If the cart needs payment. - needsShipping
boolean
: If the cart needs shipping. - hasCalculatedShipping
boolean
: If the cart has calculated shipping. - fees
array
: The cart fees. - totals
object
: The cart totals (seegetCartTotals
selector). - errors
array
: The cart errors (seegetCartErrors
selector). - paymentRequirements
object
: The payment requirements for the cart. - extensions
object
: The extensions data.
- coupons
const store = select( CART_STORE_KEY );
const cartData = store.getCartData();
Returns the shipping and billing address from the state.
object
: The current shipping and billing address with the following keys:- shippingAddress
object
: The shipping address with the following keys:- first_name
string
: The first name. - last_name
string
: The last name. - company
string
: The company name. - address_1
string
: The address line 1. - address_2
string
: The address line 2. - city
string
: The city name. - state
string
: The state name. - postcode
string
: The postcode. - country
string
: The country name.
- first_name
- billingAddress
object
: The billing address (same keys as shipping address).
- shippingAddress
const store = select( CART_STORE_KEY );
const customerData = store.getCustomerData();
Returns the shipping rates from the state.
array
: The shipping rates. They keys are as following:- id
string
: The shipping rate ID. - label
string
: The shipping rate label. - cost
string
: The shipping rate cost. - package_id
number
: The shipping rate package ID. - meta_data
array
: The shipping rate meta data. The keys are as following:- id
number
: The shipping rate meta data ID. - key
string
: The shipping rate meta data key. - value
string
: The shipping rate meta data value.
- id
- taxes
array
: The shipping rate taxes.
- id
const store = select( CART_STORE_KEY );
const shippingRates = store.getShippingRates();
Queries whether the cart needs shipping.
boolean
: True if the cart needs shipping.
const store = select( CART_STORE_KEY );
const needsShipping = store.getNeedsShipping();
Queries whether the cart shipping has been calculated.
boolean
: True if the shipping has been calculated.
const store = select( CART_STORE_KEY );
const hasCalculatedShipping = store.getHasCalculatedShipping();
Returns the cart totals from state.
object
: The current cart totals with the following keys:- total_items
string
: The sum total of items in the cart without discount, tax or shipping. - total_items_tax
string
: The total tax on all items before discount. - total_fees
string
: The total transaction fees. - total_fees_tax
string
: The tax on the total transaction fees. - total_discount
string
: The total discount applied to the cart. - total_discount_tax
string
: The tax applied to the total discount amount. - total_shipping
string
: The total shipping cost. - total_shipping_tax
string
: The tax applied to the total shipping cost. - total_tax
string
: The total tax applied to the cart. - total_price
string
: The total price of the cart including discount, tax or shipping. - tax_lines
array
of object: The tax lines:name
,price
, andrate
. - currency_code
string
: The currency code for the cart. - currency_symbol
string
: The currency symbol for the cart. - currency_minor_unit
integer
: The currency minor unit for the cart. - currency_decimal_separator
string
: The currency decimal separator for the cart. - currency_thousand_separator
string
: The currency thousand separator for the cart. - currency_prefix
string
: The currency prefix for the cart. - currency_suffix
string
: The currency suffix for the cart.
- total_items
const store = select( CART_STORE_KEY );
const cartTotals = store.getCartTotals();
Returns the cart meta from state.
object
: The current cart meta with the following keys:- updatingCustomerData
boolean
: If the customer data is being updated. - updatingSelectedRate
boolean
: If the selected rate is being updated. - isCartDataStale
boolean
: If the cart data is stale. - applyingCoupon
string
: The coupon code being applied. - removingCoupon
string
: The coupon code being removed.
- updatingCustomerData
const store = select( CART_STORE_KEY );
const cartMeta = store.getCartMeta();
Returns the cart errors from state if cart receives customer facing errors from the API.
array
: The cart errors with the following keys:- code
string
: The error code. - message
string
: The error message. - data
object
: API response data.
- code
const store = select( CART_STORE_KEY );
const cartErrors = store.getCartErrors();
Queries whether a coupon is being applied.
boolean
: True if a coupon is being applied.
const store = select( CART_STORE_KEY );
const isApplyingCoupon = store.isApplyingCoupon();
Queries whether the cart data is stale.
boolean
: True if the cart data is stale.
const store = select( CART_STORE_KEY );
const isCartDataStale = store.isCartDataStale();
Returns the coupon code being applied.
string
: The coupon code being applied.
const store = select( CART_STORE_KEY );
const couponBeingApplied = store.getCouponBeingApplied();
Queries whether a coupon is being removed.
boolean
: True if a coupon is being removed.
const store = select( CART_STORE_KEY );
const isRemovingCoupon = store.isRemovingCoupon();
Returns the coupon code being removed.
string
: The coupon code being removed.
const store = select( CART_STORE_KEY );
const couponBeingRemoved = store.getCouponBeingRemoved();
Returns a cart item from the state.
- cartItemKey
string
: The cart item key.
object
: The cart item with the following keys:- key
string
: The cart item key. - id
number
: The cart item id. - catalog_visibility
string
: The catalog visibility. - quantity_limits
object
: The quantity limits. - name
string
: The cart item name. - summary
string
: The cart item summary. - short_description
string
: The cart item short description. - description
string
: The cart item description. - sku
string
: The cart item sku. - low_stock_remaining
null
ornumber
: The low stock remaining. - backorders_allowed
boolean
indicating if backorders are allowed. - show_backorder_badge
boolean
indicating if the backorder badge should be shown. - sold_individually
boolean
indicating if the item is sold individually. - permalink
string
: The cart item permalink. - images
array
: The cart item images. - variation
array
: The cart item variation. - prices
object
: The cart item prices with the following keys:- currency_code
string
: The currency code. - currency_symbol
string
: The currency symbol. - currency_minor_unit
number
: The currency minor unit. - currency_decimal_separator
string
: The currency decimal separator. - currency_thousand_separator
string
: The currency thousand separator. - currency_prefix
string
: The currency prefix. - currency_suffix
string
: The currency suffix. - price
string
: The cart item price. - regular_price
string
: The cart item regular price. - sale_price
string
: The cart item sale price. - price_range
string
: The cart item price range.
- currency_code
- totals
object
: The cart item totals with the following keys:- currency_code
string
: The currency code. - currency_symbol
string
: The currency symbol. - currency_minor_unit
number
: The currency minor unit. - currency_decimal_separator
string
: The currency decimal separator. - currency_thousand_separator
string
: The currency thousand separator. - currency_prefix
string
: The currency prefix. - currency_suffix
string
: The currency suffix. - line_subtotal
string
: The cart item line subtotal. - line_subtotal_tax
string
: The cart item line subtotal tax. - line_total
string
: The cart item line total. - line_total_tax
string
: The cart item line total tax.
- currency_code
- key
const store = select( CART_STORE_KEY );
const cartItem = store.getCartItem( cartItemKey );
Queries whether a cart item is pending quantity.
- cartItemKey
string
: The cart item key.
boolean
: True if the cart item is pending quantity.
const store = select( CART_STORE_KEY );
const isItemPendingQuantity = store.isItemPendingQuantity( cartItemKey );
Queries whether a cart item is pending delete.
- cartItemKey
string
: The cart item key.
boolean
: True if the cart item is pending delete.
const store = select( CART_STORE_KEY );
const isItemPendingDelete = store.isItemPendingDelete( cartItemKey );
Queries whether the customer data is being updated.
boolean
: True if the customer data is being updated.
const store = select( CART_STORE_KEY );
const isCustomerDataUpdating = store.isCustomerDataUpdating();
Queries whether a shipping rate is being selected.
boolean
: True if a shipping rate is being selected.
const store = select( CART_STORE_KEY );
const isShippingRateBeingSelected = store.isShippingRateBeingSelected();
Retrieves the item keys for items whose quantity is currently being updated.
string[]
: An array with the item keys for items whose quantity is currently being updated.
const store = select( CART_STORE_KEY );
const itemsPendingQuantityUpdate = store.getItemsPendingQuantityUpdate();
Retrieves the item keys for items that are currently being deleted.
string[]
: An array with the item keys for items that are currently being deleted.
const store = select( CART_STORE_KEY );
const itemsPendingDelete = store.getItemsPendingDelete();
We're hiring! Come work with us!
🐞 Found a mistake, or have a suggestion? Leave feedback about this document here.