src/Controller/CartController.php line 152

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Model\DataObject\Sample;
  4. use App\Model\DataObject\VirtualProduct;
  5. use App\Services\CartService;
  6. use App\Services\CheckoutService;
  7. use App\Services\StockService;
  8. use Elements\Bundle\TrackingBundle\Tracking\Tracker\GoogleAnalytics4;
  9. use Elements\Bundle\TrackingBundle\Tracking\TrackingManager;
  10. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  11. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\AbstractProduct;
  12. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
  13. use Pimcore\Log\ApplicationLogger;
  14. use Pimcore\Model\DataObject;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class CartController extends AbstractController
  19. {
  20.     protected TrackingManager $trackingManager;
  21.     public function __construct(ApplicationLogger $applicationLogger, protected CartService $cartServiceFactory $factory)
  22.     {
  23.         parent::__construct($applicationLogger);
  24.         $this->trackingManager $factory->getTrackingManager();
  25.     }
  26.     /**
  27.      * @Route("/{_locale}/cart", name="cart_list")
  28.      */
  29.     public function list(Factory $factory): Response
  30.     {
  31.         $cart $this->cartService->getCart();
  32.         $this->trackingManager->trackCartView($cart);
  33.         return $this->render('cart/list.html.twig', [
  34.             'cart' => $cart
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/{_locale}/cart/update", name="cart_update")
  39.      * @param Request $request
  40.      * @return Response
  41.      */
  42.     public function update(Request $request): Response
  43.     {
  44.         $success false;
  45.         $productId $request->request->get('productId');
  46.         $product DataObject::getById($productId);
  47.         if ($product instanceof CheckoutableInterface) {
  48.             $this->cartService->updateCartItem($product$request->request->get('amount'1));
  49.             $success true;
  50.         }
  51.         $response = [
  52.             'success' => $success,
  53.             'count' => $this->cartService->getCartCount(),
  54.             'cartContent' => $this->renderView('cart/items.html.twig', [
  55.                 'cart' => $this->cartService->getCart()
  56.             ])
  57.         ];
  58.         return $this->json($response200, [
  59.             'X-Robots-Tag' => 'noindex'
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/{_locale}/cart/reorder/{order}", name="cart_reorder")
  64.      */
  65.     public function reorder(DataObject\OnlineShopOrder $order)
  66.     {
  67.         foreach($order->getItems() as $item) {
  68.             $product $item->getProduct();
  69.             if ($product instanceof Sample && $product->isActive()) {
  70.                 $this->cartService->addToCart($product$item->getAmount(), $item->getComment());
  71.             }
  72.         }
  73.         $this->cartService->limitItemCountToMax();
  74.         return $this->redirectToRoute('cart_list');
  75.     }
  76.     /**
  77.      * @Route("/{_locale}/cart/add", name="cart_add")
  78.      */
  79.     public function addToCart(Request $requestStockService $stockService): \Symfony\Component\HttpFoundation\JsonResponse
  80.     {
  81.         $id $request->get('id'0);
  82.         $product DataObject::getById($id);
  83.         $success true;
  84.         if ($product instanceof CheckoutableInterface) {
  85.             try {
  86.                 $comment = [
  87.                     'detailUrl' => $request->get('detailUrl'null)
  88.                 ];
  89.                 $this->cartService->addToCart($product, (int) $request->get('amount'1), json_encode($comment));
  90.             } catch (\Exception $e) {
  91.                 $success false;
  92.             }
  93.             //TODO: get event to view
  94.             $trackingData = [];
  95.             foreach ($this->trackingManager->getActiveTrackers() as $tracker) {
  96.                 if ($tracker instanceof GoogleAnalytics4) {
  97.                     $trackingData $tracker->createGA4ProductInfoArray($product'add_to_cart', (int)$request->get('amount'1));
  98.                 }
  99.             }
  100.         } else {
  101.             $success false;
  102.         }
  103.         $response = [
  104.             'success' => $success,
  105.             'count' => $this->cartService->getCartCount(),
  106.             'cartListContent' => $this->renderView('cart/ajax.html.twig', [
  107.                 'cart' => $this->cartService->getCart()
  108.             ]),
  109.             'detailContent' => $this->renderView('cart/detail/cart-detail-content.html.twig', [
  110.                 'product' => $product,
  111.                 'contactButton' => true,
  112.                 'contactAreaId' => 'contact-form',
  113.                 'cartButton' => $product !== null,
  114.             ]),
  115.             'trackingData' => $trackingData
  116.         ];
  117.         return $this->json($response200, [
  118.             'X-Robots-Tag' => 'noindex'
  119.         ]);
  120.     }
  121.     /**
  122.      * @Route("/{_locale}/cart/delete/{itemKey}", name="cart_delete")
  123.      */
  124.     public function delete(string $itemKey) {
  125.         $item $this->cartService->getCart()->getItem($itemKey);
  126.         $this->cartService->removeCartItem($itemKey);
  127.         $this->trackingManager->trackCartProductActionRemove($this->cartService->getCart(), $item?->getProduct(), $item?->getCount());
  128.         $this->trackingManager->forwardTrackedCodesAsFlashMessage();
  129.         return $this->redirectToRoute('cart_list');
  130.     }
  131.     /**
  132.      * @Route("/{_locale}/cart/ajax", name="cart_ajax")
  133.      */
  134.     public function ajax(): \Symfony\Component\HttpFoundation\JsonResponse
  135.     {
  136.         return $this->json([
  137.             'success' => true,
  138.             'count' => $this->cartService->getCartCount(),
  139.             'cartListContent' => $this->renderView('cart/ajax.html.twig', [
  140.                 'cart' => $this->cartService->getCart()
  141.             ])
  142.         ], 200, [
  143.             'X-Robots-Tag' => 'noindex'
  144.         ]);
  145.     }
  146. }