src/Controller/CartController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Classe\Cart;
  4. use App\Repository\ProductRepository;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class CartController extends AbstractController
  10. {
  11. /**
  12. * @Route("/mon-panier/{motif}", name="app_cart", defaults={"motif"=null})
  13. */
  14. public function index(Cart $cart, $motif): Response
  15. {
  16. If($motif = "annulation"){
  17. $this->addFlash(
  18. 'info',
  19. 'Paiement annuler : Vous pouvez mettre à jour votre panier et votre commande'
  20. );
  21. }
  22. return $this->render('cart/index.html.twig', [
  23. 'cart' => $cart->getCart(),
  24. 'totalwt' => $cart->getTotalwt()
  25. ]);
  26. }
  27. /**
  28. * @Route("/cart/add/{id}", name="app_cart_add")
  29. */
  30. public function add($id, Cart $cart, ProductRepository $productRepository, Request $request): Response
  31. {
  32. $product = $productRepository->findOneById($id);
  33. $cart->add($product);
  34. $this->addFlash(
  35. 'success',
  36. "Le produit est correctement ajouter dans votre panier"
  37. );
  38. return $this->redirect($request->headers->get('referer'));
  39. }
  40. /**
  41. * @Route("/cart/decrease/{id}", name="app_cart_decrease")
  42. */
  43. public function decrease($id, Cart $cart): Response
  44. {
  45. $cart->decrease($id);
  46. $this->addFlash(
  47. 'success',
  48. "Le produit est correctement retirer dans votre panier"
  49. );
  50. return $this->redirectToRoute('app_cart');
  51. }
  52. // Vider le panier
  53. /**
  54. * @Route("/cart/remove", name="app_cart_remove")
  55. */
  56. public function remove(Cart $cart): Response
  57. {
  58. $cart->remove();
  59. return $this->redirectToRoute('app_home');
  60. }
  61. }