src/Controller/Front/CartController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  9. use App\Manager\CartManager;
  10. use App\Entity\Cart;
  11. use App\Entity\CartItem;
  12. use App\Entity\Product;
  13. class CartController extends FrontController
  14. {
  15. public function block(CartManager $cartMgr)
  16. {
  17. $cart = $cartMgr->getCart();
  18. // foreach ($cart->getItems() as $item){
  19. // dump($item->getProduct());
  20. // }
  21. // dump($cart);
  22. return $this->render('front/cart/block.html.twig', [
  23. 'cart' => $cart
  24. ]);
  25. }
  26. /**
  27. * @Route("/{_locale}/cart/details", name="cart_details", requirements={"_locale":"fr|en"}, options={"utf8": true})
  28. */
  29. public function details(CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr, \App\Manager\CustomerManager $customerMgr)
  30. {
  31. $cart = $cartMgr->getCart();
  32. $gifts = $em->getRepository('App:Product')->getGifts();
  33. $priceMgr->updateCartPrice($cart);
  34. $discounts = [];
  35. foreach($cart->getItems() as $item){
  36. $product = $item->getProduct();
  37. $discounts[$product->getId()] = $priceMgr->getDiscountsByProduct($product);
  38. }
  39. return $this->render('front/cart/details.html.twig', [
  40. 'cart' => $cart,
  41. 'gifts' => $gifts,
  42. 'discounts' => $discounts
  43. ]);
  44. }
  45. /**
  46. * @Route("/{_locale}/cart/empty", name="cart_empty")
  47. */
  48. public function empty(CartManager $cartMgr)
  49. {
  50. $cartMgr->empty();
  51. return $this->redirectToRoute('index');
  52. }
  53. /**
  54. * @Route("/{_locale}/cart/discounts", name="cart_discounts")
  55. */
  56. public function discounts(CartManager $cartMgr)
  57. {
  58. $cart = $cartMgr->getCart();
  59. $content = $this->renderView('front/cart/discounts.html.twig', [
  60. 'cart' => $cart
  61. ]);
  62. return new JsonResponse([
  63. 'img' => $content
  64. ]);
  65. }
  66. /**
  67. * @Route("/{_locale}/cart/gift/add", name="cart_add_gift")
  68. */
  69. public function addGift(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\Session $session, \Symfony\Contracts\Translation\TranslatorInterface $trans)
  70. {
  71. $output = ['popup'=>''];
  72. $cart = $cartMgr->getCart();
  73. $qty = 1;
  74. $pid = $request->get('pid',null);
  75. if(!$pid){
  76. throw new NotFoundHttpException ();
  77. }
  78. $product = $em->getRepository(Product::class)->find($pid);
  79. if(empty($product)){
  80. throw new NotFoundHttpException ();
  81. }
  82. if(!$cart->hasGift() && $product->isGift()){
  83. if(!$cartMgr->add($product,1)){
  84. $session->getFlashBag()->add('error', $trans->trans('Une erreur s\'est produite.'));
  85. }
  86. }else{
  87. $session->getFlashBag()->add('error', $trans->trans('Vous disposez déjà d\'un cadeau dans votre panier.'));
  88. }
  89. return $this->redirectToRoute('cart_details');
  90. }
  91. /**
  92. * @Route("/{_locale}/aj/cart/add", name="cart_add")
  93. */
  94. public function add(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
  95. {
  96. $output = ['popup'=>''];
  97. $cart = $cartMgr->getCart();
  98. $qty = $request->get('qty',[]);
  99. $addResult = false;
  100. $addedProducts = [];
  101. $addedQty = [];
  102. if(!is_array($qty))
  103. throw new NotFoundHttpException ();
  104. $error = "";
  105. $success = false;
  106. $cartItem = false;
  107. $productsAdded = [];
  108. foreach($qty as $id_product=>$product_qty){
  109. if(empty($product_qty))
  110. continue;
  111. $product = $em->getRepository(Product::class)->find($id_product);
  112. if(!empty($product)){
  113. $cartItem = $cart->getItemByProduct($product);
  114. $addResult = $cartMgr->add($product,$product_qty);
  115. if($this->_checkCartResponse($addResult)){
  116. $productsAdded[] = $product;
  117. $success = true;
  118. }else{
  119. $error = "Une erreur s'est produite";
  120. }
  121. }
  122. }
  123. $priceMgr->updateCartPrice($cart);
  124. return $this->renderJsonCart($cart, $product, $priceMgr);
  125. }
  126. /**
  127. * @Route("/{_locale}/aj/cart/update", name="cart_update")
  128. */
  129. public function update(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
  130. {
  131. $output = [];
  132. $id = $request->get('id',false);
  133. $qty = $request->get('qty',0);
  134. if($id){
  135. $product = $em->getRepository(Product::class)->find($id);
  136. }
  137. if(empty($product))
  138. throw new NotFoundHttpException ();
  139. $cart = $cartMgr->update($product,$qty);
  140. $priceMgr->updateCartPrice($cart);
  141. return $this->renderJsonCart($cart, $product, $priceMgr);
  142. }
  143. /**
  144. * @Route("/{_locale}/aj/cart/address/{type}", name="cart_update_address", requirements={"type"="shipping|billing"})
  145. */
  146. public function address(Request $request, $type, CartManager $cartMgr, \App\Manager\CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em)
  147. {
  148. $id = $request->get('id',false);
  149. $type = $request->get('type',false);
  150. $address = null;
  151. $customer = $customerMgr->getCustomer();
  152. if($id){
  153. $address = $em->getRepository('App:Address')->find($id);
  154. }
  155. if($address->getCustomer()!=$customer){
  156. throw new AccessDeniedException ();
  157. }
  158. $cart = $cartMgr->getCart();
  159. $cart = $cartMgr->setAddress($address,$type);
  160. $output['success'] = true;
  161. $output['cart'] = $cart->toArray();
  162. return $this->renderJsonResponse($output);
  163. }
  164. /**
  165. * @Route("/{_locale}/aj/cart/carrier", name="cart_carrier")
  166. */
  167. public function carrier(Request $request, CartManager $cartMgr, \App\Manager\CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  168. {
  169. $id = $request->get('id',false);
  170. $cart = $cartMgr->getCart();
  171. $output['success'] = false;
  172. if($request->isMethod('POST') && preg_match('/^[a-zA-Z\-]+$/', $id)){
  173. $carrier = $em->getRepository(\App\Entity\Carrier::class)->findOneByCode($id);
  174. if($carrier){
  175. $output['success'] = true;
  176. $cart = $cartMgr->setCarrier($carrier);
  177. }else{
  178. $output['message'] = $translator->trans('Transporteur inconnu...');
  179. }
  180. }
  181. $output['cart'] = $cart->toArray();
  182. return $this->renderJsonResponse($output);
  183. }
  184. /**
  185. * @Route("/{_locale}/aj/cart/remove/{id}", name="cart_remove", requirements={"id"="\d+"})
  186. */
  187. public function delete($id, CartManager $cartMgr, \App\Manager\CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
  188. {
  189. $output = [
  190. 'success' => false
  191. ];
  192. $product = $em->getRepository(Product::class)->find($id);
  193. if($product){
  194. $cart = $cartMgr->remove($product);
  195. $success = true;
  196. $output['cart'] = $cart->toArray();
  197. }
  198. $cart = $cartMgr->getCart();
  199. $priceMgr->updateCartPrice($cart);
  200. return $this->renderJsonCart($cart, $product, $priceMgr);
  201. }
  202. /**
  203. * @Route("/{_locale}/aj/cart/add-coupon", name="cart_add_coupon")
  204. */
  205. public function addCoupon(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\DiscountManager $discountMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  206. {
  207. $output = [
  208. 'success' => false,
  209. 'error' => ''
  210. ];
  211. if($request->isMethod('POST')){
  212. $cart = $cartMgr->getCart();
  213. if($cart->getCustomer()){
  214. $code = $request->get('code');
  215. if($code){
  216. $coupon = $em->getRepository(\App\Entity\Coupon::class)->getByCode($code);
  217. if($coupon){
  218. $output['code'] = $coupon->getCode();
  219. $test = $discountMgr->checkCoupon($coupon, $cartMgr->getCart());
  220. if($test === true){
  221. $output['success'] = $cartMgr->addCoupon($coupon);
  222. $output['line'] = $this->renderView('front/cart/coupon-line.html.twig', [
  223. 'coupon'=>$coupon,
  224. 'cart'=>$cart
  225. ]);
  226. $output['cart'] = $cart->toArray();
  227. }else{
  228. $output['error'] = $test;
  229. }
  230. }else{
  231. $output['error'] = $translator->trans('Coupon non valide');
  232. }
  233. }else{
  234. $output['error'] = $translator->trans('Coupon non valide');
  235. }
  236. }else{
  237. $output['error'] = $translator->trans('Veuillez vous identifier.');
  238. }
  239. }
  240. return new JsonResponse($output);
  241. }
  242. /**
  243. * @Route("/{_locale}/aj/cart/remove-coupon", name="cart_remove_coupon")
  244. */
  245. public function removeCoupon(Request $request, CartManager $cartMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\DiscountManager $discountMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator)
  246. {
  247. $output = [
  248. 'success' => false,
  249. 'error' => ''
  250. ];
  251. if($request->isMethod('POST')){
  252. $cart = $cartMgr->getCart();
  253. if($cart->getCustomer()){
  254. $id = $request->get('id');
  255. if($id){
  256. $coupon = $em->getRepository(\App\Entity\Coupon::class)->find($id);
  257. $cart->removeCoupon($coupon);
  258. $cartMgr->save($cart);
  259. $output['success'] = true;
  260. $output['cart'] = $cart->toArray();
  261. }
  262. }
  263. }
  264. return new JsonResponse($output);
  265. }
  266. protected function getCartBlock($cart, $route='')
  267. {
  268. return $this->renderView('front/cart/block.html.twig',['cart'=>$cart,'route'=>$route]);
  269. }
  270. protected function getCartLine(CartItem $cartItem)
  271. {
  272. $discounts = [
  273. $cartItem->getProduct()->getId() => $this->priceMgr->getDiscountsByProduct($cartItem->getProduct())
  274. ];
  275. return $this->renderView('front/cart/cart-line.html.twig',[
  276. 'item' => $cartItem,
  277. 'discounts' => $discounts
  278. ]);
  279. }
  280. protected function getDiscountInfos(CartItem $cartItem)
  281. {
  282. if($cartItem->getMarketingRule()) {
  283. return $this->renderView('front/catalog/product/view/discount.html.twig',[
  284. 'discount' => $cartItem->getMarketingRule()
  285. ]);
  286. }else if($cartItem->getDiscountType()) {
  287. return $this->renderView('front/catalog/product/view/discount-type.html.twig',[
  288. 'discountType' => $cartItem->getDiscountType()
  289. ]);
  290. }
  291. return '';
  292. }
  293. protected function getDiscounts(Cart $cart)
  294. {
  295. $discounts = [];
  296. foreach ($cart->getItems() as $item) {
  297. if($item->getMarketingRule()) {
  298. $discounts[$item->getProduct()->getId()] = $this->renderView('front/catalog/product/view/discount.html.twig',[
  299. 'discount' => $item->getMarketingRule()
  300. ]);
  301. }else if($item->getDiscountType()) {
  302. $discounts[$item->getProduct()->getId()] = $this->renderView('front/catalog/product/view/discount-type.html.twig',[
  303. 'discountType' => $item->getDiscountType()
  304. ]);
  305. }
  306. }
  307. return $discounts;
  308. }
  309. protected function _checkCartResponse($response)
  310. {
  311. return is_object($response) && (get_class($response)=='App\Entity\Cart');
  312. }
  313. protected function getPriceGrids(Product $product, \App\Manager\PriceManager $priceMgr) {
  314. $output = [];
  315. $cart = $this->cartMgr->getCart();
  316. $priceGroup = $this->cartMgr->getPriceContext();
  317. if($product->hasParent()){
  318. $parent = $product->getParent();
  319. foreach($parent->getChildren() as $child){
  320. $grid = $priceMgr->getPriceGrid($child);
  321. $output[] = $this->renderView('front/catalog/product/view/price-grid.html.twig', [
  322. 'priceGrid' => $grid,
  323. 'product' => $child,
  324. 'cart' => $cart,
  325. 'priceGroup' => $priceGroup
  326. ]);
  327. }
  328. }else{
  329. $grid = $priceMgr->getPriceGrid($product);
  330. $output[] = $this->renderView('front/catalog/product/view/price-grid.html.twig', [
  331. 'priceGrid' => $grid,
  332. 'product' => $product,
  333. 'cart' => $cart,
  334. 'priceGroup' => $priceGroup
  335. ]);
  336. }
  337. return $output;
  338. }
  339. protected function renderJsonCart(Cart $cart, Product $product, \App\Manager\PriceManager $priceMgr) {
  340. $output['success'] = true;
  341. $output['cart'] = $cart->toArray();
  342. $output['product'] = $product->toArray();
  343. $output['blockCart'] = $this->getCartBlock($cart, null);
  344. $item = $cart->getItemByProduct($product);
  345. $output['cartLine'] = $item ? $this->getCartLine($item) : '';
  346. $output['discountLine'] = $item ? $this->getDiscountInfos($item) : '';
  347. $output['discounts'] = $this->getDiscounts($cart);
  348. // $output['newItem'] = $this->renderView('front/cart/block-item.html.twig', [$item => $cart->getItemByProduct($product)]);
  349. $output['currentQty'] = $cart->getItemQuantityByProduct($product);
  350. $output['priceGrids'] = $this->getPriceGrids($product, $priceMgr);
  351. return $this->renderJsonResponse($output);
  352. }
  353. }