-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathCartCommand.php
More file actions
143 lines (108 loc) · 5.16 KB
/
Copy pathCartCommand.php
File metadata and controls
143 lines (108 loc) · 5.16 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace App\Command;
use Ibexa\Contracts\Cart\CartResolverInterface;
use Ibexa\Contracts\Cart\CartServiceInterface;
use Ibexa\Contracts\Cart\Value\CartCreateStruct;
use Ibexa\Contracts\Cart\Value\CartMetadataUpdateStruct;
use Ibexa\Contracts\Cart\Value\CartQuery;
use Ibexa\Contracts\Cart\Value\EntryAddStruct;
use Ibexa\Contracts\Cart\Value\EntryUpdateStruct;
use Ibexa\Contracts\Checkout\Reorder\ReorderService;
use Ibexa\Contracts\Core\Repository\UserService;
use Ibexa\Contracts\OrderManagement\OrderServiceInterface;
use Ibexa\Contracts\ProductCatalog\CurrencyServiceInterface;
use Ibexa\Contracts\ProductCatalog\ProductServiceInterface;
use Ibexa\Core\Repository\Permission\PermissionResolver;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(
name: 'doc:cart'
)]
final class CartCommand extends Command
{
public function __construct(
private readonly PermissionResolver $permissionResolver,
private readonly UserService $userService,
private readonly CartServiceInterface $cartService,
private readonly CurrencyServiceInterface $currencyService,
private readonly ProductServiceInterface $productService,
private readonly OrderServiceInterface $orderService,
private readonly ReorderService $reorderService,
private readonly CartResolverInterface $cartResolver
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->permissionResolver->setCurrentUserReference(
$this->userService->loadUserByLogin('admin')
);
// Query for carts
$cartQuery = new CartQuery();
$cartQuery->setOwnerId(14); // carts created by User ID: 14
$cartQuery->setLimit(20); // fetch 20 carts
$cartsList = $this->cartService->findCarts($cartQuery);
$cartsList->getCarts(); // array of CartInterface objects
$cartsList->getTotalCount(); // number of returned carts
foreach ($cartsList as $cart) {
$output->writeln($cart->getIdentifier() . ': ' . $cart->getName());
}
// Get a single cart
$cart = $this->cartService->getCart('1844450e-61da-4814-8d82-9301a3df0054');
$output->writeln($cart->getName());
// Create a new cart
$currency = $this->currencyService->getCurrencyByCode('EUR');
$cartCreateStruct = new CartCreateStruct(
'Default cart',
$currency // Ibexa\Contracts\ProductCatalog\Values\CurrencyInterface
);
$cart = $this->cartService->createCart($cartCreateStruct);
$output->writeln($cart->getName()); // prints 'Default cart' to the console
// Update a cart
$newCurrency = $this->currencyService->getCurrencyByCode('PLN');
$cartUpdateMetadataStruct = new CartMetadataUpdateStruct();
$cartUpdateMetadataStruct->setName('New name');
$cartUpdateMetadataStruct->setCurrency($newCurrency);
$updatedCart = $this->cartService->updateCartMetadata($cart, $cartUpdateMetadataStruct);
$output->writeln($updatedCart->getName()); // prints 'New name' to the console
// Empty a cart
$this->cartService->emptyCart($cart);
// Validate a cart
$violationList = $this->cartService->validateCart($cart); // Symfony\Component\Validator\ConstraintViolationListInterface
// Add product to a cart
$product = $this->productService->getProduct('desk1');
$entryAddStruct = new EntryAddStruct($product);
$entryAddStruct->setQuantity(10);
$cart = $this->cartService->addEntry($cart, $entryAddStruct);
$entry = $cart->getEntries()->first();
$output->writeln($entry->getProduct()->getName()); // prints product name to the console
// Remove an entry from a cart
// find entry you would like to remove from cart
$entry = $cart->getEntries()->first();
$cart = $this->cartService->removeEntry($cart, $entry); // updated Cart object
// Update entry in a cart
$entryUpdateStruct = new EntryUpdateStruct(5);
$entryUpdateStruct->setQuantity(10);
$cart = $this->cartService->updateEntry(
$cart,
$entry,
$entryUpdateStruct
); // updated Cart object
// Delete a cart permanently
$this->cartService->deleteCart($cart);
// Get the order with items that should be reordered
$orderIdentifier = '2e897b31-0d7a-46d3-ba45-4eb65fe02790';
$order = $this->orderService->getOrderByIdentifier($orderIdentifier);
// Get the cart to merge
$existingCart = $this->cartResolver->resolveCart();
$reorderCart = $this
->reorderService
->addToCartFromOrder($order, $this->reorderService->createReorderCart($order));
// Merge the carts into the target cart and delete the merged carts
$reorderCart = $this->cartService->mergeCarts($reorderCart, true, $existingCart);
return self::SUCCESS;
}
}