lib/boab/ecommerce-bundle/src/Controller/CartController.php line 56

Open in your IDE?
  1. <?php
  2. namespace Boab\EcommerceBundle\Controller;
  3. use Boab\CmsBundle\Controller\BaseController;
  4. use Boab\CmsBundle\Exception\BoabCmsException;
  5. use Boab\EcommerceBundle\Form\CartType;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Boab\EcommerceBundle\Manager\CartManagerInterface;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpKernel\Exception\HttpException;
  11. use Symfony\Component\Serializer\SerializerInterface;
  12. use Fbeen\SettingsBundle\Service\SettingsHelper;
  13. class CartController extends BaseController
  14. {
  15.     private $cartManager;
  16.     private $serializer;
  17.     private $settingsHelper;
  18.     public function __construct(
  19.         CartManagerInterface $cartManager
  20.         SerializerInterface $serializer,
  21.         SettingsHelper $settingsHelper
  22.     )
  23.     {
  24.         $this->cartManager $cartManager;
  25.         $this->serializer $serializer;
  26.         $this->settingsHelper $settingsHelper;
  27.         
  28.     }
  29.     public function indexAction(Request $request)       
  30.     {
  31.         $cart $this->cartManager->getCart();
  32.         $form $this->createForm(CartType::class, $cart,[
  33.             "action" => $this->router->generate("shopping_cart"),
  34.         ]);
  35.         $form->handleRequest($request);
  36.         //dump($form);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             if ($form->getClickedButton() === $form->get('clear')){
  39.                 $this->cartManager->destroyCart();
  40.                 return $this->redirectToRoute('shopping_cart');
  41.             }
  42.             $cart $form->getData();
  43.             $this->cartManager->save($cart);
  44.             return $this->redirectToRoute('shopping_checkout');
  45.         }
  46.         //dump($form);
  47.         $taxRate $this->settingsHelper->getSetting('app_tax');
  48.         return $this->render('@Ecommerce/Main/cart.html.twig', [
  49.             'cart' => $cart,
  50.             'form' => $form->createView(),
  51.             'tax'=> $taxRate,
  52.             'pageTitle' => 'Cart',
  53.             'sub_total' => $this->cartManager->getSubtotal(),
  54.             'tax_charges' => $this->cartManager->getCharges(),
  55.             'grand_total' => $this->cartManager->getGrandTotal()
  56.         ]);
  57.     }
  58.     public function  addToCartAction(Request $request)
  59.     {
  60.         $productId $request->get('productId');
  61.         try{
  62.             if(!$this->cartManager->itemExists($productId)){
  63.                 $this->cartManager->addToCart($productId, (int)$request->get('qty'));
  64.             }else{
  65.                 $this->cartManager->updateItem($productId, (int)$request->get('qty'));
  66.             }
  67.             $cart $this->cartManager->getCart();
  68.             $data $this->serializer->normalize($cart);
  69.             if($request->isXmlHttpRequest()){
  70.                 return new JsonResponse([
  71.                     'status' => 'success',
  72.                     'message' => 'Cart updated successfully',
  73.                     ...$data
  74.                 ]);
  75.             }
  76.             return $this->redirect($this->router->generate('shopping_cart'));
  77.         }catch(\Exception $e){
  78.             throw new BoabCmsException($e->getMessage(), 500);
  79.         }
  80.     }
  81.     
  82.     public function updateAction(Request $request)
  83.     {
  84.         $cart $this->cartManager->getCart();
  85.         if($cart->isEmpty()){
  86.             throw new HttpException(400'The cart session has expired');
  87.         }
  88.         try{
  89.             $products $request->get("products");
  90.             $this->cartManager->updateItems($products);
  91.             //$data = $this->serializer->normalize($cart, 'json');
  92.             return $this->redirect($this->router->generate('shopping_cart'));
  93.         }catch(\Exception $e){
  94.             throw new BoabCmsException($e->getMessage(), 500);
  95.         }
  96.     }
  97.     
  98.     public function removeAction(Request $requeststring $productId)
  99.     {
  100.         if(!$this->cartManager->itemExists($productId)){
  101.             throw new NotFoundHttpException();
  102.         }
  103.         $this->cartManager->removeItem($productId);
  104.         if(!$this->cartManager->hasItems()){
  105.             $this->cartManager->destroyCart();
  106.         }
  107.         $cart $this->cartManager->getCart();
  108.         $data $this->serializer->normalize($cart);
  109.         return new JsonResponse([
  110.             "status" => "success",
  111.             "message"=>"Product deleted fro the cart successfully",
  112.             ...$data
  113.         ]);
  114.     }
  115.         
  116.     public function emptyAction(Request $request)
  117.     {
  118.         $this->cartManager->destroyCart();
  119.         return $this->redirect($this->router->generate('shopping_cart'));
  120.     }
  121.     public function getCart(Request $request)
  122.     {
  123.         $cart $this->cartManager->getCart();
  124.         $data $this->serializer->normalize($cart);
  125.         return new JsonResponse($data);
  126.     }
  127.     public function updateProductAction(Request $requeststring $productId)
  128.     {
  129.         $cart $this->cartManager->getCart();
  130.         if($cart->isEmpty()){
  131.             throw new HttpException(400'The cart session has expired');
  132.         }
  133.         
  134.         if(!$this->cartManager->itemExists($productId)){
  135.             throw new HttpException(400'The product does not extsts in the cart');
  136.         }
  137.         try{
  138.             $postData json_decode($request->getContent(), true);
  139.             $this->cartManager->updateItem($productId$postData['qty']??1);
  140.             $data $this->serializer->normalize($cart);
  141.             return new JsonResponse([
  142.                 'status' => 'sucess',
  143.                 'message' => 'Product updated successfully',
  144.                 ...$data,
  145.             ]);
  146.         }catch(\Exception $e){
  147.             $this->logger->error($e->getMessage(), ['eception'=>$e]);
  148.             throw new HttpException(400'An unknown error occured while updating product.');
  149.         }
  150.     }
  151. }