The cart component is a foundation of the Commerce offering delivered as part of Ibexa DXP. It covers actions related to the creation and handling of a list of products that the buyer intends to purchase.
The component exposes the following:
- PHP API that allows for managing carts and cart entries, or checking cart validity
- REST API that helps get cart and products information over HTTP
- Twig functions that enable checking whether product can be added to cart and formatting the price
There is no specific configuration related to the cart component. All configuration is done at the checkout and storefront level.
Cart constructor takes the following arguments:
userId- by default, read from the header's meta element with
name="UserId", where variable type must be integer
currencyCode- by default, read from the header's meta element with
lang- by default, read from the document element's
Cart data handling¶
Cart data is handled by two storages, depending on whether the buyer is anonymous or has been authenticated. Information that relates to anonymous users is stored in the PHP session, while registered user data is stored in a database.
By default, anonymous users can add items to cart, but to display the cart view, they have to log in and transition into an authenticated user.
For information about roles and permissions that control access to the cart, see Permission use cases.
Cart data merging¶
When a buyer browses the storefront anonymously and fills the cart with items, anonymous cart data is stored in the PHP session storage. Then, when an anonymous user logs into the storefront, cart data from the PHP session storage is persisted and merged with any cart information that might already exist in the database for this authenticated user.
If no previous cart data exists, a new cart is created.
Cart data validation¶
When a buyer tries to add products to the cart, increase cart item quantity or proceed to checkout, the cart component performs cart item validation and checks whether:
- the product is available
- the requested quantity of product is available
- the product is available at a price in the currency selected for the cart
From the front-end perspective, the cart consists of a main
and several widgets.
Cart service object¶
Cart service object stores cart entry data and a cart summary, which contains additional entry data, like, for example, formatted gross price or validation errors.
The object exposes several methods, which you can use to get and modify cart entries.
Only one instance of a
Cart service object can be created.
When cart data is changed or loaded, the
ibexa-cart:cart-data-changed event is triggered on
The reference to the Cart is sent in the event's
1 2 3 4 5 6 7
The Cart package provides
Ibexa\Contracts\Cart\CartServiceInterface Symfony service,
which is the entrypoint for calling the backend API.
You can import the service using the following code:
Use the service in your code as follows:
Every cart service function returns a
Promise object with a parsed response.
When the request is not
OK, it can throw an error with the response
loadUserCarts(ownerId)- loads 10 user carts
loadCartSummary(cartIdentifier)- load cart summary data
createCart(currencyCode)- creates a new cart
deleteCart(cartIdentifier)- deletes the cart
createCartEntry(cartIdentifier, productCode, quantity)- creates a new cart entry for the specified product
updateProductQuantity(cartIdentifier, entryIdentifier, quantity)- updates product quantity to a new value
deleteCartEntry(cartIdentifier, entryIdentifier)- deletes cart entry
emptyCart(cartIdentifier)- empties cart by removing all entries, returns Promise
To import and initialize cart (without extending it or passing any options), add the following:
1 2 3
Change request before sending¶
Before every request is sent by
ibexa-cart:prepare-request event is triggered on
document, so you can change request object by assigning a new one to
1 2 3 4 5 6 7
Ibexa DXP comes with a number of components that are interfaces to various functionalities exposed by the Cart. For a list of default components available in the storefront, see Default UI components.
To customize your store, you can override the Twig templates and extend their logic. For more information, see Customize storefront layout.