Shopping list APIs¶
The shopping list APIs allow managing shopping lists. The cart APIs includes methods to move products from cart to shopping list and vice versa.
About the default shopping list¶
There is one default shopping list per user. This default shopping list is created only when a user uses it for the first time.
The default shopping list is created by \Ibexa\Contracts\ShoppingList\ShoppingListServiceInterface::getOrCreateDefaultShoppingList().
For example, starting to use the default list from REST API will create it if it doesn't exist, as during a call
to POST /shopping-list/default/entries
or POST /cart/{identifier}/move-to-shopping-list.
Note that default isn't the default shopping list identifier. Each user's default shopping list has a unique identifier, a hash string like 01234567-89ab-cdef-0123-456789abcdef.
When a user has permissions to create shopping lists shopping_list/create,
they can always create a default shopping list, regardless of the maximum shopping list count per user configuration max_lists_per_user.
PHP API¶
In the Ibexa\Contracts\ShoppingList namespace are the interfaces to manipulate shopping lists.
The Ibexa\Contracts\ShoppingList\ShoppingListServiceInterface defines methods to
create, get, find, update, clear, and delete shopping lists, and to add, get, move, and remove entries.
List and search shopping lists¶
Shopping list search can be done with
ShoppingListServiceInterface::findShoppingLists() method
with a ShoppingListQuery
built with criteria from the Criterion namespace
implementing the CriterionInterface,
and with sort clauses from the SortClause namespace.
To get all shopping lists (of the current user or of the whole repository depending on the current user limitation), use the search method without criterion:
1 | |
For more information about the shopping list search, see Shopping list criteria, and Shopping list sort clauses references.
Manage shopping lists entries¶
Methods editing the shopping list first store the change in the persistence layer then return the updated shopping list object.
If you forgot to retrieve this result in your variable, the local object isn't synchronized with the database.
In the following example, if some assignments ($list =) are removed, the dumped $list object doesn't contain the stored shopping list at that time.
If only the middle assignment is removed, the last dumped variable contains the up-to-date shopping list.
1 2 3 4 5 6 | |
ShoppingListService::addEntries(),
an exception is thrown if at least product is already in the shopping list and no entries are added to the list.
The following example adds products to a shopping list while avoiding error on duplicated entries. In this example the duplicates are ignored, but you could extend it to, for example, notify the user about each found duplicate.
1 2 3 4 5 6 7 8 9 10 11 | |
The following example moves products from a source shopping list to a target shopping list after filtering out products already in the target list:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Transfer between shopping list and cart¶
Interactions between shopping list and cart are managed by
Ibexa\Contracts\Cart\CartShoppingListTransferServiceInterface
The following example starts with an empty cart and an empty shopping list, then adds a product to the shopping list and copies it twice to the cart. It continues with moving the whole cart to an empty list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Events¶
When the shopping list service methods are called, event are dispatched before and after the action so its parameters or results can be customized. For more information, see Shopping list event reference.
There is no specific event for the transfer operations.
- When adding from shopping list to cart, the
Ibexa\Contracts\Cart\Event\BeforeAddEntryEventandIbexa\Contracts\Cart\Event\AddEntryEventare dispatched for each entry that wasn't previously in the cart. - When moving from cart to shopping list, single
Ibexa\Contracts\ShoppingList\Event\BeforeAddEntriesEventandIbexa\Contracts\ShoppingList\Event\AddEntriesEventevents are dispatched for the batch of entries, thenIbexa\Contracts\Cart\Event\BeforeRemoveEntryEventandIbexa\Contracts\Cart\Event\BeforeRemoveEntryEventare dispatched for each entry removed from the cart.
REST API¶
The REST API provides resources for managing shopping lists and their entries, as well as for moving products between the cart and the shopping list.
These resources start with /shopping-list/*.
In Symfony's dev environment, you can consult and test the REST API at /api/ibexa/v2/doc#/Shopping%20List.
The following REST example uses curl and jq to:
- log in a user
- search for the default shopping list to get its identifier
- clear the default shopping list if it exists using its identifier
- add a product to the default shopping list
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Transfer between shopping list and cart¶
You can use:
POST /shopping-list/{identifier}/add-entries-to-cartto add some shopping list entries to the default cartPOST /shopping-list/{identifier}/add-entries-to-cart/{cartIdentifier}to add some shopping list entries to a specific cartPOST /shopping-list/{identifier}/add-to-cartto add all entries from a shopping list to the default cartPOST /shopping-list/{identifier}/add-to-cart/{cartIdentifier}to add all entries from a shopping list to a specific cartPOST /cart/{identifier}/move-entries-to-shopping-listto move some cart entries to the default shopping listPOST /cart/{identifier}/move-entries-to-shopping-list/{shoppingListIdentifier}to move some cart entries to a specific shopping listPOST /cart/{identifier}/move-to-shopping-listto move all entries from a cart to the default shopping listPOST /cart/{identifier}/move-to-shopping-list/{shoppingListIdentifier}to move all entries from a cart to a specific shopping list