/
CartItemUpdateRoute.php
54 lines (44 loc) · 2.12 KB
/
CartItemUpdateRoute.php
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
<?php declare(strict_types=1);
namespace Shopware\Core\Checkout\Cart\SalesChannel;
use Shopware\Core\Checkout\Cart\AbstractCartPersister;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\CartCalculator;
use Shopware\Core\Checkout\Cart\Event\AfterLineItemQuantityChangedEvent;
use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
use Shopware\Core\Checkout\Cart\LineItemFactoryRegistry;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
#[Route(defaults: ['_routeScope' => ['store-api']])]
#[Package('checkout')]
class CartItemUpdateRoute extends AbstractCartItemUpdateRoute
{
/**
* @internal
*/
public function __construct(private readonly AbstractCartPersister $cartPersister, private readonly CartCalculator $cartCalculator, private readonly LineItemFactoryRegistry $lineItemFactory, private readonly EventDispatcherInterface $eventDispatcher)
{
}
public function getDecorated(): AbstractCartItemUpdateRoute
{
throw new DecorationPatternException(self::class);
}
#[Route(path: '/store-api/checkout/cart/line-item', name: 'store-api.checkout.cart.update-lineitem', methods: ['PATCH'])]
public function change(Request $request, Cart $cart, SalesChannelContext $context): CartResponse
{
$itemsToUpdate = $request->request->all('items');
/** @var array<mixed> $item */
foreach ($itemsToUpdate as $item) {
$this->lineItemFactory->update($cart, $item, $context);
}
$cart->markModified();
$cart = $this->cartCalculator->calculate($cart, $context);
$this->cartPersister->save($cart, $context);
$this->eventDispatcher->dispatch(new AfterLineItemQuantityChangedEvent($cart, $itemsToUpdate, $context));
$this->eventDispatcher->dispatch(new CartChangedEvent($cart, $context));
return new CartResponse($cart);
}
}