src/Controller/CartController.php line 92

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Products;
  4. use App\Repository\ProductsRepository;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. #[Route('/cart', name: 'cart_')]
  9. class CartController extends AbstractController
  10. {
  11.   #[Route('/', name: 'index')]
  12.   public function index(SessionInterface $session, ProductsRepository $productsRepository)
  13.   {
  14.     $panier = $session->get('panier', []);
  15.     //On initialise des variables
  16.     $data = [];
  17.     $total = 0;
  18.     foreach ($panier as $id => $quantity) {
  19.       $product = $productsRepository->find($id);
  20.       $data[] = [
  21.         'product' => $product,
  22.         'quantity' => $quantity
  23.       ];
  24.       $total += $product->getPrice() * $quantity; 
  25.     }
  26.     return $this->render('cart/index.html.twig', compact('data', 'total'));
  27.   }  
  28.   #[Route('/add/{id}', name: 'add')]
  29.   public function add(Products $product, SessionInterface $session)
  30.   {
  31.     //On récupère l'id du produit
  32.     $id = $product->getId();
  33.     //On récupère le panier existant
  34.     $panier = $session->get('panier', []);
  35.     //On ajoute le produit dans le panier s'il n'y est pas encore
  36.     //Sinon on incrémente sa quantité
  37.     if(empty($panier[$id])){
  38.       $panier[$id] = 1;
  39.     }else{
  40.       $panier[$id]++;
  41.     }
  42.     $session->set('panier', $panier);
  43.     
  44.     //On redirige vers la page du panier
  45.     return $this->redirectToRoute('cart_index');
  46.   }
  47.  
  48.   #[Route('/remove/{id}', name: 'remove')]
  49.   public function remove(Products $product, SessionInterface $session)
  50.   {
  51.     //On récupère l'id du produit
  52.     $id = $product->getId();
  53.     //On récupère le panier existant
  54.     $panier = $session->get('panier', []);
  55.     //On retire le produit du panier s'il n'y a qu'1 exemplaire
  56.     //Sinon on décrémente sa quantité
  57.     if(!empty($panier[$id])){
  58.       if($panier[$id] > 1){
  59.         $panier[$id] --;
  60.       }else{
  61.         unset($panier[$id]);
  62.       }
  63.     }
  64.     $session->set('panier', $panier);
  65.     
  66.     //On redirige vers la page du panier
  67.     return $this->redirectToRoute('cart_index');
  68.   }
  69.   #[Route('/delete/{id}', name: 'delete')]
  70.   public function delete(Products $product, SessionInterface $session)
  71.   {
  72.     //On récupère l'id du produit
  73.     $id = $product->getId();
  74.     //On récupère le panier existant
  75.     $panier = $session->get('panier', []);
  76.     if(!empty($panier[$id])){
  77.         unset($panier[$id]);
  78.     }
  79.     $session->set('panier', $panier);
  80.      
  81.     //On redirige vers la page du panier
  82.     return $this->redirectToRoute('cart_index');
  83.   }
  84.   #[Route('/empty', name: 'empty')]
  85.   public function empty(SessionInterface $session)
  86.   {
  87.     $session->remove('panier');
  88.     return $this->redirectToRoute('cart_index');
  89.   }  
  90. }