src/Controller/Frontend/ProductCartController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Entity\Gos\Cart;
  4. use App\Entity\Gos\ProductCart;
  5. use App\Entity\Gos\Uniqskills\Course;
  6. use App\Entity\Gos\User;
  7. use App\Utils\Cart\CartPriceCalculator;
  8. use App\Utils\CartServices;
  9. use App\Utils\FacebookPixel\Api\FacebookPixelService;
  10. use App\Utils\OrderGifts;
  11. use App\Utils\PortalSettingsService;
  12. use App\Utils\SalesManago\SalesManagoCartUtils;
  13. use App\Utils\Shipping\ShippingTypes\ShippingTypesPicker;
  14. use App\Utils\Uniqskills\DomainListener;
  15. use App\Utils\Uniqskills\MixPanel\MixPanelService;
  16. use App\Utils\Uniqskills\SuggestedCoursesService;
  17. use App\Utils\UpsellingUtils;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Psr\Log\LoggerInterface;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Routing\Annotation\Route;
  24. class ProductCartController extends AbstractController
  25. {
  26.     private $em;
  27.     private $cartServices;
  28.     private $giftsService;
  29.     private $domainListener;
  30.     private $salesManagoCartUtils;
  31.     private $suggestedCoursesService;
  32.     private $mixPanelService;
  33.     private $portalSettingsService;
  34.     private $facebookPixelService;
  35.     private $shippingTypesPicker;
  36.     public function  __construct(
  37.         EntityManagerInterface $entityManager,
  38.         CartServices $cartServices,
  39.         OrderGifts $orderGifts,
  40.         DomainListener $domainListener,
  41.         SalesManagoCartUtils $salesManagoCartUtils,
  42.         SuggestedCoursesService $suggestedCoursesService,
  43.         MixPanelService $mixPanelService,
  44.         PortalSettingsService $portalSettingsService,
  45.         FacebookPixelService $facebookPixelService,
  46.         ShippingTypesPicker $shippingTypesPicker
  47.     ) {
  48.         $this->em $entityManager;
  49.         $this->cartServices $cartServices;
  50.         $this->giftsService $orderGifts;
  51.         $this->domainListener $domainListener;
  52.         $this->salesManagoCartUtils $salesManagoCartUtils;
  53.         $this->suggestedCoursesService $suggestedCoursesService;
  54.         $this->mixPanelService $mixPanelService;
  55.         $this->portalSettingsService $portalSettingsService;
  56.         $this->facebookPixelService $facebookPixelService;
  57.         $this->shippingTypesPicker $shippingTypesPicker;
  58.     }
  59.     /**
  60.      * @Route("/cart/view/{newCartHash}", name="fmProductCartFrontendView")
  61.      * @Route("/{_locale}/cart/{newCartHash}", name="fmUniqskillsCartIndex",
  62.      *     host="www.%us_url%",
  63.      *     requirements={"newCartHash"="^(?!.*(address|participants)).*$", "_locale"="^(?!.*(en)|(pl)).*$"}))
  64.      */
  65.     public function viewAction(
  66.         Request $request,
  67.         LoggerInterface $logger,
  68.         UpsellingUtils $upsellingUtils,
  69.         CartPriceCalculator $cartPriceCalculator,
  70.         $newCartHash null,
  71.         $iframe true
  72.     )
  73.     {
  74.         // task GOS-1569
  75.         if (in_array($request->getLocale(), ['es''pt''ru''de''ua']) == true) return $this->redirectToRoute('fmUniqskillsHomePage');
  76.         /** @var Cart $cart*/
  77.         $findCart       $this->cartServices->findCart($this->getUser(), $newCartHash);
  78.         $cart           $findCart['cart'];
  79.         $mergeCarts     $findCart['mergeCarts'];
  80.         $requestSource  $this->domainListener->getRequestSource($request);
  81.         $isUniqskills   $request->get('_route') === 'fmUniqskillsCartIndex';
  82.         $suggestedCourses null;
  83.         if (null === $portal $this->portalSettingsService->getPortalSettings())
  84.         {
  85.             $logger->debug('No portal in product cart. cartHash: ' $cart->getHash(), [
  86.                 'userAgent' => $_SERVER["HTTP_USER_AGENT"] ?? null,
  87.                 'referer'   => $_SERVER["HTTP_REFERER"] ?? null,
  88.             ]);
  89.             return new Response("Portal settings not set"Response::HTTP_BAD_REQUEST);
  90.         }
  91.         if ($cart->hasProducts())
  92.         {
  93.             if ($request->isMethod('POST'))
  94.             {
  95.                 $this->giftsService->processCartProducts($cart);
  96.                 if ($cart->hasEventProduct() || ($isUniqskills && $cart->hasUniqskillsMultiUserCourse()))
  97.                 {
  98.                     if ($isUniqskills)
  99.                     {
  100.                         return $this->redirectToRoute('fmUniqskillsCartParticipants');
  101.                     }
  102.                     return $this->redirectToRoute('fmOrderParticipants', ['source' => $requestSource]);
  103.                 }
  104.                 if ($isUniqskills)
  105.                 {
  106.                     return $this->redirectToRoute('fmUniqskillsCartAddress');
  107.                 }
  108.                 return $this->redirectToRoute('fmOrderUserBase', ['source' => $requestSource]);
  109.             }
  110.             if ($isUniqskills)
  111.             {
  112.                 $suggestedCourses = [];
  113.                 $isProlongation $request->query->get('isProlongation');
  114.                 /** @var ProductCart $productCart */
  115.                 foreach ($cart->getProductCart() as $productCart)
  116.                 {
  117.                     if ($productCart->getProductVariant() === null)
  118.                     {
  119.                         continue;
  120.                     }
  121.                     if ($productCart->getProductVariant()->getCourses())
  122.                     {
  123.                         /** @var Course $course */
  124.                         $course $productCart->getProductVariant()->getCourses()->get(0);
  125.                         $suggestedCourses array_merge($suggestedCourses$this->suggestedCoursesService->findSuggestedCourses($course));
  126.                     }
  127.                 }
  128.                 if ($isProlongation == 'true')
  129.                 {
  130.                     $this->cartServices->findPaymentSystemForProlongationVariant($cart);
  131.                     $this->em->refresh($cart);
  132.                 }
  133.             }
  134.         }
  135.         $this->salesManagoCartUtils->saveCartEvent($cart$this->getUser());
  136.         if (!($this->getUser() instanceof User))
  137.         {
  138.             $request->getSession()->set('name''noLogin');
  139.         }
  140.         if ($portal->checkIsCmsLiterkaPortal() === true)
  141.         {
  142.             $productVariantsList = [];
  143.             foreach ($cart->getProductCart() as $productCart)
  144.             {
  145.                 if ($productCart->getProductVariant() != null$productVariantsList[] = $productCart->getProductVariant();
  146.             }
  147.             $literkaProducts $this->cartServices->getLiterkaProduct($portal$productVariantsList);
  148.         }
  149.         $cart->setShippingType(null);
  150.         //only in new cart user can select shipping type
  151.         if ($portal->getEnableNewBasket())
  152.         {
  153.             $selectableShippingTypes $this->shippingTypesPicker->find($cart);
  154.             $this->cartServices->setShippingType($cart$selectableShippingTypes);
  155.         }
  156.         $price $cartPriceCalculator->getCartPrice($cart);
  157.         $parameters = array(
  158.             'cart'              => $cart,
  159.             'mergeCarts'        => $mergeCarts,
  160.             'newCartHash'       => $newCartHash,
  161.             'price'             => isset($price) ? $price null,
  162.             'error'             => isset($price['error']) ? $price['error'] : false,
  163.             'iframe'            => $iframe,
  164.             'source'            => $requestSource,
  165.             'suggestedCourses'  => $suggestedCourses,
  166.             'literkaProducts'   => isset($literkaProducts) ? $literkaProducts null,
  167.             'upsellings'        => $upsellingUtils->prepareForCart($cart, [], false),
  168.             'additionalTextProductVariant' => $this->cartServices->getAdditionalTextProductVariant($cart)
  169.         );
  170.         if ($mergeCarts && isset($findCart['oldCart']))
  171.         {
  172.             $parameters['oldCart'] = $findCart['oldCart'];
  173.         }
  174.         if ($isUniqskills)
  175.         {
  176.             $this->mixPanelService->sendCartRenderEvent([
  177.                 'step'     => 'initial',
  178.                 'products' => $price ?? null,
  179.             ], $this->getUser());
  180.             $this->facebookPixelService->setEvent('InitiateCheckout'$cart->getId())->request();
  181.             return $this->render('uniqskills/cart/index.html.twig'$parameters);
  182.         }
  183.         if ($portal->getUseFbPixelInIframe() === true)
  184.         {
  185.             $this->facebookPixelService->setEvent('InitiateCheckout'$cart->getId())->request();
  186.         }
  187.         if ($_ENV['APP_ENV'] === 'dev')
  188.         {
  189.             $parameters['newCartNextStep'] = $_ENV['NEW_CART_DEV_URL'] . '/' $cart->getHash();
  190.         }
  191.         else
  192.         {
  193.             $domain str_replace('http://'''$portal->getDomain());
  194.             $domain str_replace('https://'''$domain);
  195.             $domain str_replace('www.'''$domain);
  196.             $newCartLink sprintf('%s://www.zamowienia.%s/%s''https'$domain$cart->getHash());
  197.             $parameters['newCartNextStep'] = $newCartLink;
  198.         }
  199.         
  200.         if ($request->isXmlHttpRequest() || $iframe === false)
  201.         {
  202.             if ($portal->getEnableNewBasket())
  203.             {
  204.                 return $this->render('frontend/product_cart_2/ajax_view.html.twig'$parameters);
  205.             }
  206.             return $this->render('frontend/product_cart/ajax_view.html.twig'$parameters);
  207.         }
  208.         if ($portal->getEnableNewBasket())
  209.         {
  210.             return $this->render('frontend/product_cart_2/view.html.twig'$parameters);
  211.         }
  212.         return $this->render('frontend/product_cart/view.html.twig'$parameters);
  213.     }
  214.     /**
  215.      * @Route("/cart/mergeCart", name="fmProductCartFrontendMergeCart")
  216.      */
  217.     public function mergeCartAction(Request $requestMergeCart $mergeCart): Response
  218.     {
  219.         $userCartHash   = ($this->getUser() && $this->getUser()->getCart()) ? $this->getUser()->getCart()->getHash() : null;
  220.         $newCartHash    $request->request->get('newCartHash');
  221.         $cookieCartHash $request->cookies->get('cartHash');
  222.         $method         $request->request->get('method'false);
  223.         if ($method)
  224.         {
  225.             if ($method === 'mergeCart')
  226.             {
  227.                 $mergeCart->mergeCart(
  228.                     empty($newCartHash) ? $cookieCartHash $newCartHash,
  229.                     $userCartHash
  230.                 );
  231.             }
  232.             else
  233.             {
  234.                 $mergeCart->takeNewCart(
  235.                     empty($newCartHash) ? $cookieCartHash $newCartHash,
  236.                     $userCartHash
  237.                 );
  238.             }
  239.             return new Response('ok');
  240.         }
  241.         return new Response('error');
  242.     }
  243.     public function cartStepAction(Request $request)
  244.     {
  245.         $cartSteps = array(
  246.             => array(
  247.                 'name'  => 'cart.steps.cart',
  248.                 'path'  => 'fmProductCartFrontendView',
  249.                 'class' => ''
  250.             ),
  251.             => array(
  252.                 'name'  => 'cart.steps.login',
  253.                 'path'  => 'fmOrderUserBase',
  254.                 'class' => ''
  255.             ),
  256.             => array(
  257.                 'name'  => 'cart.steps.address',
  258.                 'path'  => 'fmOrderAddressBase',
  259.                 'class' => ''
  260.             ),
  261.             => array(
  262.                 'name'  => 'cart.steps.summary',
  263.                 'path'  => 'fmOrderSummaryBase',
  264.                 'class' => ''
  265.             ),
  266.         );
  267.         $findCart $this->cartServices->findCart($this->getUser());
  268.         /** @var Cart $cart */
  269.         $cart $findCart['cart'];
  270.         if ($cart->hasEventProduct())
  271.         {
  272.             $cartSteps[1] = array(
  273.                 'name'  => 'cart.steps.participants',
  274.                 'path'  => 'fmOrderParticipants',
  275.                 'class' => ''
  276.             );
  277.         }
  278.         ksort($cartSteps);
  279.         $active true;
  280.         foreach ($cartSteps as $key => $cartStep)
  281.         {
  282.             if ($cartStep['path'] == $request->get('_route'))
  283.             {
  284.                 $cartSteps[$key]['class'] = 'active currently-active';
  285.                 $active false;
  286.             }
  287.             else if ($active)
  288.             {
  289.                 $cartSteps[$key]['class'] = 'active hidden-xs';
  290.             }
  291.             else
  292.             {
  293.                 $cartSteps[$key]['class'] = 'hidden-xs';
  294.             }
  295.         }
  296.         $colSm = (12 count($cartSteps));
  297.         
  298.         return $this->render('frontend/product_cart_2/cart_step.html.twig', array(
  299.             'cartSteps' => $cartSteps,
  300.             'colSm'     => (int)$colSm
  301.         ));
  302.     }
  303. }