1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\OrderBundle\Controller\Front; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use WellCommerce\Bundle\CatalogBundle\Entity\Product; |
18
|
|
|
use WellCommerce\Bundle\CatalogBundle\Entity\Variant; |
19
|
|
|
use WellCommerce\Bundle\CoreBundle\Controller\Front\AbstractFrontController; |
20
|
|
|
use WellCommerce\Bundle\OrderBundle\Entity\OrderProduct; |
21
|
|
|
use WellCommerce\Bundle\OrderBundle\Exception\AddCartItemException; |
22
|
|
|
use WellCommerce\Bundle\OrderBundle\Manager\OrderProductManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class CartController |
26
|
|
|
* |
27
|
|
|
* @author Adam Piotrowski <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class CartController extends AbstractFrontController |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var OrderProductManager |
33
|
|
|
*/ |
34
|
|
|
protected $manager; |
35
|
|
|
|
36
|
|
|
public function indexAction(): Response |
37
|
|
|
{ |
38
|
|
|
$order = $this->getOrderProvider()->getCurrentOrder(); |
39
|
|
|
$form = $this->formBuilder->createForm($order, [ |
40
|
|
|
'validation_groups' => ['cart'], |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
if ($form->handleRequest()->isSubmitted()) { |
44
|
|
|
if ($form->isValid()) { |
45
|
|
|
$this->manager->updateResource($order); |
46
|
|
|
|
47
|
|
|
return $this->getRouterHelper()->redirectTo('front.cart.index'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (count($form->getError())) { |
51
|
|
|
$this->getFlashHelper()->addError('client.flash.registration.error'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->displayTemplate('index', [ |
56
|
|
|
'form' => $form, |
57
|
|
|
'order' => $order, |
58
|
|
|
'elements' => $form->getChildren(), |
59
|
|
|
]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function addAction(Product $product, Variant $variant = null, int $quantity = 1): Response |
63
|
|
|
{ |
64
|
|
|
$variants = $product->getVariants(); |
65
|
|
|
$order = $this->getOrderProvider()->getCurrentOrder(); |
66
|
|
|
$previousQuantity = $this->manager->getCartQuantity($product, $variant, $order); |
67
|
|
|
|
68
|
|
|
if ($variants->count() && (null === $variant || false === $variants->contains($variant))) { |
69
|
|
|
return $this->redirectToRoute('front.product.view', ['id' => $product->getId()]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$this->manager->addProductToOrder( |
74
|
|
|
$product, |
75
|
|
|
$variant, |
76
|
|
|
$quantity, |
77
|
|
|
$order |
78
|
|
|
); |
79
|
|
|
} catch (AddCartItemException $exception) { |
80
|
|
|
return $this->jsonResponse([ |
81
|
|
|
'error' => $exception->getMessage(), |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$expectedQuantity = $previousQuantity + $quantity; |
86
|
|
|
$currentQuantity = $this->manager->getCartQuantity($product, $variant, $order); |
87
|
|
|
$addedQuantity = 0; |
88
|
|
|
if ($currentQuantity >= $expectedQuantity) { |
89
|
|
|
$addedQuantity = $currentQuantity - $previousQuantity; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$category = $product->getCategories()->first(); |
93
|
|
|
$recommendations = $this->get('product.helper')->getProductRecommendationsForCategory($category); |
94
|
|
|
|
95
|
|
|
$basketModalContent = $this->renderView('WellCommerceOrderBundle:Front/Cart:add.html.twig', [ |
96
|
|
|
'product' => $product, |
97
|
|
|
'order' => $order, |
98
|
|
|
'recommendations' => $recommendations, |
99
|
|
|
'previousQuantity' => $previousQuantity, |
100
|
|
|
'currentQuantity' => $currentQuantity, |
101
|
|
|
'addedQuantity' => $addedQuantity, |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
$cartPreviewContent = $this->renderView('WellCommerceOrderBundle:Front/Cart:preview.html.twig'); |
105
|
|
|
|
106
|
|
|
return $this->jsonResponse([ |
107
|
|
|
'basketModalContent' => $basketModalContent, |
108
|
|
|
'cartPreviewContent' => $cartPreviewContent, |
109
|
|
|
'templateData' => [], |
110
|
|
|
'productTotal' => [ |
111
|
|
|
'quantity' => $order->getProductTotal()->getQuantity(), |
112
|
|
|
'grossAmount' => $this->getCurrencyHelper()->convertAndFormat($order->getProductTotal()->getGrossPrice()), |
113
|
|
|
], |
114
|
|
|
]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function editAction(Request $request, OrderProduct $orderProduct, int $quantity): Response |
118
|
|
|
{ |
119
|
|
|
$success = true; |
120
|
|
|
$message = null; |
121
|
|
|
$order = $this->getOrderProvider()->getCurrentOrder(); |
122
|
|
|
|
123
|
|
|
try { |
124
|
|
|
$this->manager->changeOrderProductQuantity( |
125
|
|
|
$orderProduct, |
126
|
|
|
$order, |
127
|
|
|
$quantity |
128
|
|
|
); |
129
|
|
|
} catch (\Exception $e) { |
130
|
|
|
$success = false; |
131
|
|
|
$message = $e->getMessage(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($request->isXmlHttpRequest()) { |
135
|
|
|
return $this->jsonResponse([ |
136
|
|
|
'success' => $success, |
137
|
|
|
'message' => $message, |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->redirectResponse($request->headers->get('referer')); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function deleteAction(OrderProduct $orderProduct): Response |
145
|
|
|
{ |
146
|
|
|
try { |
147
|
|
|
$this->manager->deleteOrderProduct( |
148
|
|
|
$orderProduct, |
149
|
|
|
$this->getOrderProvider()->getCurrentOrder() |
150
|
|
|
); |
151
|
|
|
} catch (\Exception $e) { |
152
|
|
|
$this->getFlashHelper()->addError($e->getMessage()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $this->redirectToAction('index'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|