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 Doctrine\Common\Collections\Collection; |
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
|
|
|
private int $itemsProcessed = 0; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
FlashBagInterface $flashBag, |
39
|
|
|
TranslatorInterface $translator, |
40
|
|
|
OrderItemQuantityModifierInterface $itemQuantityModifier, |
41
|
|
|
OrderModifierInterface $orderModifier, |
42
|
|
|
OrderRepositoryInterface $orderRepository |
43
|
|
|
) { |
44
|
|
|
$this->flashBag = $flashBag; |
45
|
|
|
$this->translator = $translator; |
46
|
|
|
$this->itemQuantityModifier = $itemQuantityModifier; |
47
|
|
|
$this->orderModifier = $orderModifier; |
48
|
|
|
$this->orderRepository = $orderRepository; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __invoke(AddSelectedProductsToCart $addSelectedProductsToCartCommand): void |
52
|
|
|
{ |
53
|
|
|
$this->addSelectedProductsToCart($addSelectedProductsToCartCommand->getWishlistProducts()); |
54
|
|
|
|
55
|
|
|
$this->addFlashMessage(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function addSelectedProductsToCart(Collection $wishlistProducts): void |
59
|
|
|
{ |
60
|
|
|
/** @var AddWishlistProduct $wishlistProduct */ |
61
|
|
|
foreach ($wishlistProducts as $wishlistProduct) { |
62
|
|
|
if (!$wishlistProduct->isSelected()) { |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!$this->isInStock($wishlistProduct)) { |
67
|
|
|
continue; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->addProductToWishlist($wishlistProduct); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function isInStock(AddWishlistProduct $wishlistProduct): bool |
75
|
|
|
{ |
76
|
|
|
$cartItem = $wishlistProduct->getCartItem()->getCartItem(); |
77
|
|
|
|
78
|
|
|
if ($wishlistProduct->getCartItem()->getCartItem()->getVariant()->isInStock()) { |
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
$message = sprintf(' "%s" does not have sufficient stock.', $cartItem->getProductName()); |
|
|
|
|
82
|
|
|
$this->flashBag->add('error', $this->translator->trans($message)); |
83
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function addProductToWishlist(AddWishlistProduct $wishlistProduct): void |
88
|
|
|
{ |
89
|
|
|
$cart = $wishlistProduct->getCartItem()->getCart(); |
90
|
|
|
$cartItem = $wishlistProduct->getCartItem()->getCartItem(); |
91
|
|
|
|
92
|
|
|
if (0 === $cartItem->getQuantity()) { |
93
|
|
|
$this->itemQuantityModifier->modify($cartItem, 1); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->orderModifier->addToOrder($cart, $cartItem); |
97
|
|
|
$this->orderRepository->add($cart); |
98
|
|
|
++$this->itemsProcessed; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function addFlashMessage(): void |
102
|
|
|
{ |
103
|
|
|
if (0 < $this->itemsProcessed) { |
104
|
|
|
$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_selected_wishlist_items_to_cart')); |
105
|
|
|
|
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->flashBag->add('error', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products')); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|