-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartController.php
More file actions
134 lines (109 loc) · 3.7 KB
/
CartController.php
File metadata and controls
134 lines (109 loc) · 3.7 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace App\Controller;
use App\Entity\Cart;
use App\Entity\Product;
use App\Repository\CartRepository;
use App\Repository\ProductRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
class CartController extends AbstractController
{
private $cartRepository;
private $productRepository;
private $session;
public function __construct(
CartRepository $cartRepository,
ProductRepository $productRepository,
SessionInterface $session
) {
$this->cartRepository = $cartRepository;
$this->productRepository = $productRepository;
$this->session = $session;
}
/**
* @Route("/cart", name="cart_view")
*/
public function viewCart()
{
$sessionId = $this->session->getId() ?: 'default_session';
$cartData = $this->cartRepository->findCartBySessionId($sessionId);
if (empty($cartData)) {
return $this->render('cart/view.html.twig', [
'items' => [],
'total' => 0,
]);
}
$cart = $cartData[0];
$items = json_decode($cart['items'], true);
$products = [];
$total = 0;
foreach ($items as $productId => $quantity) {
$product = $this->productRepository->find($productId);
if ($product) {
$products[] = [
'product' => $product,
'quantity' => $quantity,
'subtotal' => $product->getPrice() * $quantity,
];
$total += $product->getPrice() * $quantity;
}
}
return $this->render('cart/view.html.twig', [
'items' => $products,
'total' => $total,
]);
}
/**
* @Route("/cart/add/{id}", name="cart_add")
*/
public function addToCart($id, Request $request)
{
$product = $this->productRepository->find($id);
if (!$product) {
$this->addFlash('error', 'Product not found!');
return $this->redirectToRoute('product_list');
}
$quantity = $request->query->get('quantity', 1);
if (!is_numeric($quantity)) {
$quantity = 1;
}
$sessionId = $this->session->getId() ?: 'default_session';
$this->cartRepository->addItemToCart($sessionId, $id, $quantity);
$this->addFlash('success', 'Product added to cart!');
return $this->redirectToRoute('product_list');
}
/**
* @Route("/cart/remove/{id}", name="cart_remove")
*/
public function removeFromCart($id)
{
$sessionId = $this->session->getId();
$cart = $this->cartRepository->findOneBy(['sessionId' => $sessionId]);
if ($cart) {
$items = $cart->getItems();
unset($items[$id]);
$cart->setItems($items);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($cart);
$entityManager->flush();
}
return $this->redirectToRoute('cart_view');
}
/**
* @Route("/cart/clear", name="cart_clear")
*/
public function clearCart()
{
$sessionId = $this->session->getId();
$cart = $this->cartRepository->findOneBy(['sessionId' => $sessionId]);
if ($cart) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($cart);
$entityManager->flush();
}
return $this->redirectToRoute('product_list');
}
}