src/Controller/Shop/CartController.php line 353

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Controller\Shop;
  7. use App\Model\DataObject\Customer;
  8. use App\Model\Shop\Event\EventProduct;
  9. use App\Model\Shop\Event\ShopDynamicPrice;
  10. use App\Model\Shop\Merchandise\MerchandiseProduct;
  11. use App\Model\Shop\Ticket\ShopTicketCatalog;
  12. use App\Service\Shop\ShopService;
  13. use App\Service\TrackingService;
  14. use Carbon\Carbon;
  15. use Elements\Bundle\TicketShopFrameworkBundle\Model\DataObject\TicketConsumerCategory;
  16. use Elements\Bundle\TicketShopFrameworkBundle\Model\DataObject\TicketProduct;
  17. use Pimcore\Model\DataObject\CustomerContact;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. /**
  23.  * Class CheckoutController
  24.  *
  25.  * @Route("/{_locale}/shop/cart")
  26.  *
  27.  * @package App\Controller\Shop
  28.  */
  29. class CartController extends AbstractShopController
  30. {
  31.     /**
  32.      * @Route("/list", name="cart_list")
  33.      *
  34.      * @param Request $request
  35.      *
  36.      * @return Response
  37.      */
  38.     public function listAction(Request $request): Response
  39.     {
  40.         $trackingManager $this->factory->getTrackingManager();
  41.         /** @phpstan-ignore-next-line */
  42.         $trackingManager->trackcartView($this->getCart());
  43.         return $this->render('shop/cart/list.html.twig');
  44.     }
  45.     /**
  46.      * @Route("/add/ticket", name="cart_add_ticket")
  47.      *
  48.      * @param Request $request
  49.      * @param TranslatorInterface $translator
  50.      * @param ShopService $shopService
  51.      *
  52.      * @return Response
  53.      *
  54.      * @throws \Exception
  55.      */
  56.     public function addTicketAction(Request $requestTranslatorInterface $translatorShopService $shopServiceTrackingService $trackingService): Response
  57.     {
  58.         //        $success = false;
  59.         $cart $this->getCart();
  60.         $addedItems = [];
  61.         $data null;
  62.         if ($request->getContentType() == 'json') {
  63.             $data json_decode($request->getContent(), true);
  64.         }
  65.         if (isset($data['configurations'])) {
  66.             foreach ($data['configurations'] as $config) {
  67.                 $insurance null;
  68.                 $params = [];
  69.                 $originalTicketProduct TicketProduct::getById((int)$config['option']);
  70.                 $ticketProduct TicketProduct::getById((int)$config['upgradeOption']);
  71.                 $ticketCatalog ShopTicketCatalog::getById((int)$config['upgradeCatalog']);
  72.                 $consumerCategory TicketConsumerCategory::getById((int)$config['mappedPriceGroup']);
  73.                 if (isset($config['dateStart'])) {
  74.                     $ticketStartDate Carbon::create($config['dateStart']);
  75.                 } else {
  76.                     $ticketStartDate Carbon::today();
  77.                     if ($ticketProduct->isSeasonTicket() && $ticketCatalog->getCurrentDateRangeStartDate()?->greaterThan($ticketStartDate)) {
  78.                         $ticketStartDate $ticketCatalog->getCurrentDateRangeStartDate();
  79.                     }
  80.                 }
  81.                 if (array_key_exists('personId'$config)) {
  82.                     if ($person CustomerContact::getById((int)$config['personId'])) {
  83.                         $params['person'] = $person;
  84.                     } elseif ($customer Customer::getById((int)$config['personId'])) {
  85.                         $params['person'] = $customer;
  86.                     }
  87.                 }
  88.                 $params['isUpgradeOption'] = $config['upgradeOption'] != $config['option'];
  89.                 $params['originalProduct'] = $originalTicketProduct;
  90.                 //                $isInsuranceAllowed = true;
  91.                 if ($insuranceId $config['insurance']) {
  92.                     $insurance TicketProduct::getById((int)$insuranceId);
  93.                     //                    $skidataProduct = $ticketProduct->getSkidataProduct();
  94.                     //                    $isInsuranceAllowed = $shopService->isInsuranceAllowed($insurance, $skidataProduct, $consumerCategory->getSkidataConsumerForSkidataProduct($skidataProduct));
  95.                 }
  96.                 if ($consumerCategory) { //&& $isInsuranceAllowed
  97.                     $addedItemsPerCategory $shopService->addTicketItemsToCartPerConsumer(
  98.                         $ticketProduct,
  99.                         $consumerCategory,
  100.                         1,
  101.                         $insurance,
  102.                         $ticketCatalog,
  103.                         $cart,
  104.                         $ticketStartDate,
  105.                         $params
  106.                     );
  107.                     $addedItems array_merge($addedItems$addedItemsPerCategory);
  108.                     //                    $success = true;
  109.                 }
  110.             }
  111.             if ($addedItems) {
  112.                 $trackingManager $this->factory->getTrackingManager();
  113.                 $arr $trackingService->correctItemQuantity($cart$addedItems);
  114.                 $productCount $arr['count'];
  115.                 $products $arr['products'];
  116.                 foreach ($productCount as $key => $count) {
  117.                     $trackingManager->trackCartProductActionAdd($cart$products[$key], $count);
  118.                 }
  119.                 $trackingManager->forwardTrackedCodesAsFlashMessage();
  120.             }
  121.         }
  122.         $jsonResponse = [
  123.             'success' => true,
  124.             'message' => $translator->trans('cart.cart-add-successful'), // $translator->trans('cart.cart-add-error'),
  125.             'ticketCount' => count($addedItems),
  126.             'cartItemsCounter' => $cart->getItemAmount(),
  127.         ];
  128.         return $this->json($jsonResponse);
  129.     }
  130.     /**
  131.      * @Route("/add/event", name="cart_add_event")
  132.      *
  133.      * @param Request $request
  134.      * @param TranslatorInterface $translator
  135.      * @param ShopService $shopService
  136.      *
  137.      * @return Response
  138.      *
  139.      * @throws \Exception
  140.      */
  141.     public function addEventAction(Request $requestTranslatorInterface $translatorShopService $shopServiceTrackingService $trackingService): Response
  142.     {
  143.         $cart $this->getCart();
  144.         $data null;
  145.         if ($request->getContentType() == 'json') {
  146.             $data json_decode($request->getContent(), true);
  147.         }
  148.         $product EventProduct::getById($data['activityId']);
  149.         $allAddedItems = [];
  150.         if (isset($data['configurations'])) {
  151.             foreach ($data['configurations'] as $config) {
  152.                 $quantity $config['amount'];
  153.                 $priceObj ShopDynamicPrice::getById($config['priceGroup']);
  154.                 $selectedDate Carbon::parse($config['selectedDate']);
  155.                 if (isset($config['selectedTime'])) {
  156.                     $selectedDate->setTimeFromTimeString($config['selectedTime']);
  157.                 }
  158.                 if ($addedItem $shopService->addEventToCartPerPriceObject($product$priceObj$cart$selectedDate$quantity)) {
  159.                     $allAddedItems[] = $addedItem;
  160.                 }
  161.             }
  162.             if ($allAddedItems) {
  163.                 $trackingManager $this->factory->getTrackingManager();
  164.                 foreach ($allAddedItems as $value) {
  165.                     $trackingManager->trackCartProductActionAdd($cart$value->getProduct(), $value->getCount());
  166.                 }
  167.                 $trackingManager->forwardTrackedCodesAsFlashMessage();
  168.             }
  169.         }
  170.         return $this->json([
  171.             'success' => true,
  172.             'message' => $translator->trans('cart.cart-add-successful'),
  173.             'ticketCount' => count($allAddedItems),
  174.             'cartItemsCounter' => $cart->getItemAmount(),
  175.         ]);
  176.     }
  177.     /**
  178.      * @Route("/add/merch", name="cart_add_merch")
  179.      *
  180.      * @param Request $request
  181.      * @param TranslatorInterface $translator
  182.      * @param ShopService $shopService
  183.      *
  184.      * @return Response
  185.      *
  186.      * @throws \Exception
  187.      */
  188.     public function addMerchAction(Request $requestTranslatorInterface $translatorShopService $shopServiceTrackingService $trackingService): Response
  189.     {
  190.         if ($variantId $request->get('variant')) {
  191.             $merch MerchandiseProduct::getById(intval($variantId));
  192.         } else {
  193.             $merch MerchandiseProduct::getById(intval($request->get('id')));
  194.         }
  195.         $trackingData = [];
  196.         if ($merch instanceof MerchandiseProduct) {
  197.             $product $shopService->addMerchandiseToCart($merch$request->get('amount') ?: 1$this->getCart());
  198.             $trackingData $trackingService->createGA4ProductInfoArray($product->getProduct(), 'add_to_cart'$request->get('amount') ?: 1);
  199.         }
  200.         $jsonResponse = [
  201.             'success' => true,
  202.             'message' => $translator->trans('cart.cart-add-successful'),
  203.             'ticketCount' => $request->get('amount') ?: 1,
  204.             'cartItemsCounter' => $this->getCart()->getItemAmount(),
  205.         ];
  206.         if (!empty($trackingData)) {
  207.             $jsonResponse['responseTrackingData'] = [
  208.                 'gtm' => [
  209.                     'datalayer' => $trackingData,
  210.                 ],
  211.             ];
  212.         }
  213.         return $this->json($jsonResponse);
  214.     }
  215.     /**
  216.      * @Route("/remove-product/{itemKey}", name="cart_remove_product_modal")
  217.      *
  218.      * @param Request $request
  219.      * @param string $itemKey
  220.      * @param string $cartName
  221.      *
  222.      * @return Response
  223.      */
  224.     public function deleteProductModal(Request $requeststring $itemKeystring $cartName 'cart'): Response
  225.     {
  226.         $form $this->createFormBuilder([])->getForm();
  227.         $form->handleRequest($request);
  228.         if ($form->isSubmitted()) {
  229.             $cart $this->getCart($cartName);
  230.             $trackingManager $this->factory->getTrackingManager();
  231.             $cartItem $cart->getItem($itemKey);
  232.             $trackingManager->trackCartProductActionRemove($cart$cartItem->getProduct(), $cartItem->getCount());
  233.             $trackingManager->forwardTrackedCodesAsFlashMessage();
  234.             $cart->removeItem($itemKey);
  235.             if ($cart->isEmpty()) {
  236.                 /** @phpstan-ignore-next-line */
  237.                 $cart->setCheckoutData('cashback'null);
  238.             }
  239.             $cart->save();
  240.             //reset cashback
  241.             return $this->redirect($request->get('redirectUri'));
  242.         }
  243.         $html $this->renderTemplate('shop/includes/cart/delete-product-modal.html.twig', [
  244.             'form' => $form->createView(),
  245.             'itemName' => $request->get('itemName'),
  246.         ])->getContent();
  247.         return $this->json([
  248.             'success' => true,
  249.             'html' => $html,
  250.         ]);
  251.     }
  252.     /**
  253.      * @Route("/remove-product-group", name="cart_remove_product_group_modal")
  254.      *
  255.      * @param Request $request
  256.      * @param string $cartName
  257.      *
  258.      * @return Response
  259.      */
  260.     public function deleteProductGroupModal(Request $requeststring $cartName 'cart'): Response
  261.     {
  262.         $form $this->createFormBuilder([])->getForm();
  263.         $form->handleRequest($request);
  264.         if ($form->isSubmitted()) {
  265.             $cart $this->getCart($cartName);
  266.             $cartItemKeys explode(','$request->get('itemKeys'));
  267.             foreach ($cartItemKeys as $cartItemKey) {
  268.                 $cart->removeItem(htmlspecialchars($cartItemKey));
  269.             }
  270.             if ($cart->isEmpty()) {
  271.                 /** @phpstan-ignore-next-line */
  272.                 $cart->setCheckoutData('cashback'null);
  273.             }
  274.             $cart->save();
  275.             //reset cashback
  276.             return $this->redirect($request->get('redirectUri'));
  277.         }
  278.         $html $this->renderTemplate('shop/includes/cart/delete-product-modal.html.twig', [
  279.             'form' => $form->createView(),
  280.             'itemName' => $request->get('itemName'),
  281.             'isGroupDelete' => $request->get('isGroupDelete'),
  282.         ])->getContent();
  283.         return $this->json([
  284.             'success' => true,
  285.             'html' => $html,
  286.         ]);
  287.     }
  288.     /**
  289.      * @Route("/clear", name="cart_clear")
  290.      *
  291.      * @return Response
  292.      */
  293.     public function clearCart(): Response
  294.     {
  295.         $cart $this->getCart();
  296.         $cart->clear();
  297.         $cart->save();
  298.         return $this->redirectToRoute('cart_list');
  299.     }
  300.     /**
  301.      * @Route("/cart-overlay", name="cart_overlay")
  302.      *
  303.      * @return Response
  304.      */
  305.     public function cartOverlay(): Response
  306.     {
  307.         $cart $this->getCart();
  308.         if ($cart->getItemAmount() == 0) {
  309.             $html $this->renderTemplate('shop/includes/cart/cart-overlay-result-empty.html.twig')->getContent();
  310.         } else {
  311.             $html $this->renderTemplate('shop/includes/cart/cart-overlay-result.html.twig')->getContent();
  312.         }
  313.         return $this->json([
  314.             'success' => true,
  315.             'html' => $html,
  316.         ]);
  317.     }
  318. }