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\Controller; |
12
|
|
|
|
13
|
|
|
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface; |
14
|
|
|
use Sylius\Bundle\OrderBundle\Controller\OrderItemController as BaseController; |
15
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
16
|
|
|
use Sylius\Component\Order\CartActions; |
17
|
|
|
use Symfony\Component\Form\SubmitButton; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
21
|
|
|
|
22
|
|
|
final class OrderItemController extends BaseController |
23
|
|
|
{ |
24
|
|
|
public function addAction(Request $request): Response |
25
|
|
|
{ |
26
|
|
|
$cart = $this->getCurrentCart(); |
27
|
|
|
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request); |
28
|
|
|
|
29
|
|
|
$this->isGrantedOr403($configuration, CartActions::ADD); |
30
|
|
|
/** @var OrderItemInterface $orderItem */ |
31
|
|
|
$orderItem = $this->newResourceFactory->create($configuration, $this->factory); |
32
|
|
|
|
33
|
|
|
$this->getQuantityModifier()->modify($orderItem, 1); |
34
|
|
|
|
35
|
|
|
/** @var string $formType */ |
36
|
|
|
$formType = $configuration->getFormType(); |
37
|
|
|
|
38
|
|
|
$form = $this->getFormFactory()->create( |
39
|
|
|
$formType, |
40
|
|
|
$this->createAddToCartCommand($cart, $orderItem), |
41
|
|
|
$configuration->getFormOptions() |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$form->handleRequest($request); |
45
|
|
|
|
46
|
|
|
/** @var SubmitButton $addToWishlist */ |
47
|
|
|
$addToWishlist = $form->get('addToWishlist'); |
48
|
|
|
|
49
|
|
|
if ($addToWishlist->isClicked()) { |
50
|
|
|
/** @var AddToCartCommandInterface $addToCartCommand */ |
51
|
|
|
$addToCartCommand = $form->getData(); |
52
|
|
|
|
53
|
|
|
/** @var OrderItemInterface $item */ |
54
|
|
|
$item = $addToCartCommand->getCartItem(); |
55
|
|
|
$variant = $item->getVariant(); |
56
|
|
|
|
57
|
|
|
if (null === $variant) { |
58
|
|
|
throw new NotFoundHttpException('Could not find variant'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new Response($this->generateUrl('bitbag_sylius_wishlist_plugin_shop_wishlist_add_product_variant', [ |
62
|
|
|
'variantId' => $variant->getId(), |
63
|
|
|
])); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return parent::addAction($request); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|