|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file was created by developers working at BitBag |
|
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
|
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddSelectedProductsToCart; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct; |
|
15
|
|
|
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface; |
|
16
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
|
17
|
|
|
use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; |
|
18
|
|
|
use Sylius\Component\Order\Modifier\OrderModifierInterface; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
|
20
|
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface; |
|
21
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class AddSelectedProductsToCartHandler implements MessageHandlerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
private FlashBagInterface $flashBag; |
|
26
|
|
|
|
|
27
|
|
|
private TranslatorInterface $translator; |
|
28
|
|
|
|
|
29
|
|
|
private OrderItemQuantityModifierInterface $itemQuantityModifier; |
|
30
|
|
|
|
|
31
|
|
|
private OrderModifierInterface $orderModifier; |
|
32
|
|
|
|
|
33
|
|
|
private OrderRepositoryInterface $orderRepository; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct( |
|
36
|
|
|
FlashBagInterface $flashBag, |
|
37
|
|
|
TranslatorInterface $translator, |
|
38
|
|
|
OrderItemQuantityModifierInterface $itemQuantityModifier, |
|
39
|
|
|
OrderModifierInterface $orderModifier, |
|
40
|
|
|
OrderRepositoryInterface $orderRepository |
|
41
|
|
|
) { |
|
42
|
|
|
$this->flashBag = $flashBag; |
|
43
|
|
|
$this->translator = $translator; |
|
44
|
|
|
$this->itemQuantityModifier = $itemQuantityModifier; |
|
45
|
|
|
$this->orderModifier = $orderModifier; |
|
46
|
|
|
$this->orderRepository = $orderRepository; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __invoke(AddSelectedProductsToCart $addSelectedProductsToCart): void |
|
50
|
|
|
{ |
|
51
|
|
|
/** @var AddWishlistProduct $wishlistProduct */ |
|
52
|
|
|
foreach ($addSelectedProductsToCart->getWishlistProducts() as $wishlistProduct) { |
|
53
|
|
|
if (!$wishlistProduct->isSelected()) { |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** @var AddToCartCommandInterface $addToCartCommand */ |
|
58
|
|
|
$addToCartCommand = $wishlistProduct->getCartItem(); |
|
59
|
|
|
|
|
60
|
|
|
$this->handleItem($addToCartCommand); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function handleItem(AddToCartCommandInterface $addToCartCommand): void |
|
65
|
|
|
{ |
|
66
|
|
|
$cart = $addToCartCommand->getCart(); |
|
67
|
|
|
$cartItem = $addToCartCommand->getCartItem(); |
|
68
|
|
|
|
|
69
|
|
|
if (!$cartItem->getVariant()->isInStock()) { |
|
|
|
|
|
|
70
|
|
|
$message = sprintf('%s does not have sufficient stock.', $cartItem->getProductName()); |
|
|
|
|
|
|
71
|
|
|
$this->flashBag->add('error', $this->translator->trans($message)); |
|
72
|
|
|
} else { |
|
73
|
|
|
$this->itemQuantityModifier->modify($cartItem, 1); |
|
74
|
|
|
$this->orderModifier->addToOrder($cart, $cartItem); |
|
75
|
|
|
$this->orderRepository->add($cart); |
|
76
|
|
|
|
|
77
|
|
|
$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_to_cart')); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|