Skip to content

Commit 05b9dd7

Browse files
committed
shopping_list_api.md: moveEntries example
1 parent 10542d7 commit 05b9dd7

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

docs/commerce/shopping_list/shopping_list_api.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ dump($list);
4444

4545
When adding array of entries with `ShoppingListService::addEntries()` or `ShoppingListService::moveEntries()`,
4646
an exception is thrown if a product is already in the shopping list and the whole array is canceled.
47-
If you work with batch of products, filter it before adding it, or add entries one by one.
4847

49-
The following examples both add products to a shopping list while avoiding error on duplicate:
48+
The two following examples both add products to a shopping list while avoiding error on duplicate.
49+
(To stay short, this examples doesn't track down duplicates but it could be implemented for notification to the user.)
5050

5151
```php
5252
$filteredProductCodes = array_filter($desiredProductCodes, function ($productCode) use ($list) {
@@ -63,22 +63,43 @@ foreach ($desiredProductCodes as $productCode) {
6363
}
6464
```
6565

66-
TODO: How to choose which solution above to use?
66+
TODO: How to choose which solution to use between the two above?
6767

6868
`ShoppingListService::moveEntries()` doesn't return an updated shopping list because several lists might be updated.
69-
TODO: The following example merge two shopping lists into a new third one.
7069

71-
TODO: [Shopping list event reference](shopping_list_events.md)
70+
The following example moves products from a source shopping list to a target shopping list after filtering products already in the target list.
71+
Notice how the source and target lists' variables are updated from persistence after the move:
7272

73-
TODO: [`Ibexa\Contracts\Cart\CartShoppingListTransferServiceInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Cart-CartShoppingListTransferServiceInterface.html)
73+
```php
74+
$entriesToMove = [];
75+
$entriesToRemove = [];
76+
foreach ($movedProductCodes as $productCode) {
77+
if ($targetList->getEntries()->hasEntryWithProductCode($productCode)) {
78+
$entriesToRemove[] = $sourceList->getEntries()->getEntryWithProductCode($productCode);
79+
} else {
80+
$entriesToMove[] = $sourceList->getEntries()->getEntryWithProductCode($productCode);
81+
}
82+
}
83+
$this->shoppingListService->moveEntries($targetList, $entriesToMove);
84+
$targetList = $this->shoppingListService->getShoppingList($targetList->getIdentifier()); // Refresh local object from persistence
85+
$sourceList = $this->shoppingListService->removeEntries($sourceList, $entriesToRemove); // Refresh local object from persistence even if $entriesToRemove is empty
86+
```
87+
88+
When the shopping list service methods are called, event are dispatched before and after the action so its parameters or results can be customized.
89+
TODO: Event example?
90+
For more information, see [Shopping list event reference](shopping_list_events.md).
91+
92+
Interactions between shopping list and cart are managed by
93+
[`Ibexa\Contracts\Cart\CartShoppingListTransferServiceInterface`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Cart-CartShoppingListTransferServiceInterface.html)
94+
95+
TODO: example and reco. Maybe clarify duplicate handling of this case methods
7496

7597
There is no specific event for the transfer operations.
7698

7799
- When adding from shopping list to cart, the `Ibexa\Contracts\Cart\Event\BeforeAddEntryEvent` and `Ibexa\Contracts\Cart\Event\AddEntryEvent` are dispatched for each entry that weren't previously in the cart.
78100
- When moving from cart to shopping list, `Ibexa\Contracts\ShoppingList\Event\BeforeAddEntriesEvent` and `Ibexa\Contracts\ShoppingList\Event\AddEntriesEvent` are dispatched for the batch of entries that weren't already in the shopping list,
79101
then `Ibexa\Contracts\Cart\Event\BeforeRemoveEntryEvent` and `Ibexa\Contracts\Cart\Event\BeforeRemoveEntryEvent` are dispatched for each entry removed from the cart.
80102

81-
82103
## REST API
83104

84105
The REST API has several resources to manage shopping lists and their entries

0 commit comments

Comments
 (0)