src/Controller/Shop/CartController.php line 355

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 ($addedItems $shopService->addEventToCartPerPriceObject($product$priceObj$cart$selectedDate$quantity)) {
  159.                     foreach ($addedItems as $addedItem) {
  160.                         $allAddedItems[] = $addedItem;
  161.                     }
  162.                 }
  163.             }
  164.             if ($allAddedItems) {
  165.                 $trackingManager $this->factory->getTrackingManager();
  166.                 foreach ($allAddedItems as $value) {
  167.                     $trackingManager->trackCartProductActionAdd($cart$value->getProduct(), $value->getCount());
  168.                 }
  169.                 $trackingManager->forwardTrackedCodesAsFlashMessage();
  170.             }
  171.         }
  172.         return $this->json([
  173.             'success' => true,
  174.             'message' => $translator->trans('cart.cart-add-successful'),
  175.             'ticketCount' => count($allAddedItems),
  176.             'cartItemsCounter' => $cart->getItemAmount(),
  177.         ]);
  178.     }
  179.     /**
  180.      * @Route("/add/merch", name="cart_add_merch")
  181.      *
  182.      * @param Request $request
  183.      * @param TranslatorInterface $translator
  184.      * @param ShopService $shopService
  185.      *
  186.      * @return Response
  187.      *
  188.      * @throws \Exception
  189.      */
  190.     public function addMerchAction(Request $requestTranslatorInterface $translatorShopService $shopServiceTrackingService $trackingService): Response
  191.     {
  192.         if ($variantId $request->get('variant')) {
  193.             $merch MerchandiseProduct::getById(intval($variantId));
  194.         } else {
  195.             $merch MerchandiseProduct::getById(intval($request->get('id')));
  196.         }
  197.         $trackingData = [];
  198.         if ($merch instanceof MerchandiseProduct) {
  199.             $product $shopService->addMerchandiseToCart($merch$request->get('amount') ?: 1$this->getCart());
  200.             $trackingData $trackingService->createGA4ProductInfoArray($product->getProduct(), 'add_to_cart'$request->get('amount') ?: 1);
  201.         }
  202.         $jsonResponse = [
  203.             'success' => true,
  204.             'message' => $translator->trans('cart.cart-add-successful'),
  205.             'ticketCount' => $request->get('amount') ?: 1,
  206.             'cartItemsCounter' => $this->getCart()->getItemAmount(),
  207.         ];
  208.         if (!empty($trackingData)) {
  209.             $jsonResponse['responseTrackingData'] = [
  210.                 'gtm' => [
  211.                     'datalayer' => $trackingData,
  212.                 ],
  213.             ];
  214.         }
  215.         return $this->json($jsonResponse);
  216.     }
  217.     /**
  218.      * @Route("/remove-product/{itemKey}", name="cart_remove_product_modal")
  219.      *
  220.      * @param Request $request
  221.      * @param string $itemKey
  222.      * @param string $cartName
  223.      *
  224.      * @return Response
  225.      */
  226.     public function deleteProductModal(Request $requeststring $itemKeystring $cartName 'cart'): Response
  227.     {
  228.         $form $this->createFormBuilder([])->getForm();
  229.         $form->handleRequest($request);
  230.         if ($form->isSubmitted()) {
  231.             $cart $this->getCart($cartName);
  232.             $trackingManager $this->factory->getTrackingManager();
  233.             $cartItem $cart->getItem($itemKey);
  234.             $trackingManager->trackCartProductActionRemove($cart$cartItem->getProduct(), $cartItem->getCount());
  235.             $trackingManager->forwardTrackedCodesAsFlashMessage();
  236.             $cart->removeItem($itemKey);
  237.             if ($cart->isEmpty()) {
  238.                 /** @phpstan-ignore-next-line */
  239.                 $cart->setCheckoutData('cashback'null);
  240.             }
  241.             $cart->save();
  242.             //reset cashback
  243.             return $this->redirect($request->get('redirectUri'));
  244.         }
  245.         $html $this->renderTemplate('shop/includes/cart/delete-product-modal.html.twig', [
  246.             'form' => $form->createView(),
  247.             'itemName' => $request->get('itemName'),
  248.         ])->getContent();
  249.         return $this->json([
  250.             'success' => true,
  251.             'html' => $html,
  252.         ]);
  253.     }
  254.     /**
  255.      * @Route("/remove-product-group", name="cart_remove_product_group_modal")
  256.      *
  257.      * @param Request $request
  258.      * @param string $cartName
  259.      *
  260.      * @return Response
  261.      */
  262.     public function deleteProductGroupModal(Request $requeststring $cartName 'cart'): Response
  263.     {
  264.         $form $this->createFormBuilder([])->getForm();
  265.         $form->handleRequest($request);
  266.         if ($form->isSubmitted()) {
  267.             $cart $this->getCart($cartName);
  268.             $cartItemKeys explode(','$request->get('itemKeys'));
  269.             foreach ($cartItemKeys as $cartItemKey) {
  270.                 $cart->removeItem(htmlspecialchars($cartItemKey));
  271.             }
  272.             if ($cart->isEmpty()) {
  273.                 /** @phpstan-ignore-next-line */
  274.                 $cart->setCheckoutData('cashback'null);
  275.             }
  276.             $cart->save();
  277.             //reset cashback
  278.             return $this->redirect($request->get('redirectUri'));
  279.         }
  280.         $html $this->renderTemplate('shop/includes/cart/delete-product-modal.html.twig', [
  281.             'form' => $form->createView(),
  282.             'itemName' => $request->get('itemName'),
  283.             'isGroupDelete' => $request->get('isGroupDelete'),
  284.         ])->getContent();
  285.         return $this->json([
  286.             'success' => true,
  287.             'html' => $html,
  288.         ]);
  289.     }
  290.     /**
  291.      * @Route("/clear", name="cart_clear")
  292.      *
  293.      * @return Response
  294.      */
  295.     public function clearCart(): Response
  296.     {
  297.         $cart $this->getCart();
  298.         $cart->clear();
  299.         $cart->save();
  300.         return $this->redirectToRoute('cart_list');
  301.     }
  302.     /**
  303.      * @Route("/cart-overlay", name="cart_overlay")
  304.      *
  305.      * @return Response
  306.      */
  307.     public function cartOverlay(): Response
  308.     {
  309.         $cart $this->getCart();
  310.         if ($cart->getItemAmount() == 0) {
  311.             $html $this->renderTemplate('shop/includes/cart/cart-overlay-result-empty.html.twig')->getContent();
  312.         } else {
  313.             $html $this->renderTemplate('shop/includes/cart/cart-overlay-result.html.twig')->getContent();
  314.         }
  315.         return $this->json([
  316.             'success' => true,
  317.             'html' => $html,
  318.         ]);
  319.     }
  320. }