Cart API¶
Cart REST API
To learn how to manage carts with the REST API, see the REST API reference.
To get carts and work with them, use the Ibexa\Contracts\Cart\CartServiceInterface
interface.
CartService
uses two storage methods and handles switching between storages:
- carts of registered users use database-based storage
- anonymous user carts are stored in the PHP session
From the developer's perspective, carts and entries are referenced with a UUID identifier.
Get single cart by identifier¶
To access a single cart, use the CartServiceInterface::getCart
method:
1 2 3 |
|
Get multiple carts¶
To fetch multiple carts, use the CartServiceInterface::findCarts
method.
It follows the same search Query pattern as other APIs:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Create cart¶
To create a cart, use the CartServiceInterface::createCart
method and provide
it with Ibexa\Contracts\Cart\Value\CartCreateStruct
that contains metadata (name, currency, owner):
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Update cart metadata¶
You can update cart metadata after the cart is created.
You could do it to support a scenario when, for example, the user changes a currency
and the cart should recalculate all item prices to a new currency.
To update cart metadata, use the CartServiceInterface::updateCartMetadata
method:
1 2 3 4 5 6 7 8 9 10 11 |
|
You can also use this method to change cart ownership:
1 2 3 4 5 6 7 8 |
|
Delete cart¶
To delete a cart permanently, use the CartServiceInterface::deleteCart
method
and pass the CartInterface
object:
1 2 3 |
|
Empty cart¶
To remove all products from the cart in a single operation, use the
CartServiceInterface::emptyCart
method:
1 2 3 |
|
Check cart validity¶
Items in cart can become invalid, for example, when item price is unavailable
in cart currency, or the product is no longer available.
To prevent checking out a cart with invalid items, check cart validity first.
To validate the cart, use the CartServiceInterface::validateCart
method.
Validation is done with help from the symfony/validator
component, and the method
returns a Symfony\Component\Validator\ConstraintViolationListInterface
object.
1 2 3 |
|
Add entry to cart¶
To add entries (products) to the cart, create an Ibexa\Contracts\Cart\Value\EntryAddStruct
,
where you specify the requested quantity of the product.
Then pass it to the CartServiceInterface::addEntry
method:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Remove entry from cart¶
To remove an entry from the cart, use the CartServiceInterface::removeEntry
method.
1 2 3 4 5 6 7 8 9 |
|
Update entry metadata¶
Entries have their own metadata, for example, quantity.
To change entry metadata, use the CartServiceInterface::updateEntry
method
and provide it with Ibexa\Contracts\Cart\Value\EntryUpdateStruct
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|