custom/plugins/KmPitchPrint/src/Controller/WishlistController.php line 144

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace KmPitchPrint\Controller;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerWishlistProduct\CustomerWishlistProductEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEntity;
  6. use Shopware\Core\Checkout\Customer\Exception\CustomerWishlistNotFoundException;
  7. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractAddWishlistProductRoute;
  8. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLoadWishlistRoute;
  9. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractMergeWishlistProductRoute;
  10. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRemoveWishlistProductRoute;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  16. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  17. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  18. use Shopware\Core\Framework\Routing\Annotation\Since;
  19. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  20. use Shopware\Core\Framework\Uuid\Uuid;
  21. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  22. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  23. use Shopware\Storefront\Page\Wishlist\GuestWishlistPageLoader;
  24. use Shopware\Storefront\Page\Wishlist\WishlistPageLoader;
  25. use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
  26. use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoader;
  27. use Swp\ProductOptionsSix\Core\Checkout\ProductOptions\Cart\ProductOptionsCartDataCollector;
  28. use Swp\ProductOptionsSix\Core\Content\ProductOptions\SalesChannel\ProductOptionsAddToCart;
  29. use Symfony\Component\HttpFoundation\Cookie;
  30. use Symfony\Component\HttpFoundation\JsonResponse;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\Response;
  33. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  34. use Symfony\Component\Routing\Annotation\Route;
  35. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  36. use Shopware\Storefront\Controller\StorefrontController;
  37. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  38. use KmPitchPrint\Storefront\Page\Wishlist\KmWishlistGuestPageLoader;
  39. use KmPitchPrint\Events\MoveToWishlistEvent;
  40. /**
  41.  * @RouteScope(scopes={"storefront"})
  42.  */
  43. class WishlistController extends StorefrontController
  44. {
  45.     private WishlistPageLoader $wishlistPageLoader;
  46.     private AbstractLoadWishlistRoute $wishlistLoadRoute;
  47.     private AbstractAddWishlistProductRoute $addWishlistRoute;
  48.     private AbstractRemoveWishlistProductRoute $removeWishlistProductRoute;
  49.     private AbstractMergeWishlistProductRoute $mergeWishlistProductRoute;
  50.     private GuestWishlistPageLoader $guestPageLoader;
  51.     private GuestWishlistPageletLoader $guestPageletLoader;
  52.     private EventDispatcherInterface $eventDispatcher;
  53.     /**
  54.      * @var CartService
  55.      */
  56.     private $cartService;
  57.     /**
  58.      * @var KmWishlistGuestPageLoader
  59.      */
  60.     private $kMWishlistGuestPageLoader;
  61.     private $productOptionsAddToCart;
  62.     public function __construct(
  63.         WishlistPageLoader                 $wishlistPageLoader,
  64.         AbstractLoadWishlistRoute          $wishlistLoadRoute,
  65.         AbstractAddWishlistProductRoute    $addWishlistRoute,
  66.         AbstractRemoveWishlistProductRoute $removeWishlistProductRoute,
  67.         AbstractMergeWishlistProductRoute  $mergeWishlistProductRoute,
  68.         GuestWishlistPageLoader            $guestPageLoader,
  69.         GuestWishlistPageletLoader         $guestPageletLoader,
  70.         EventDispatcherInterface           $eventDispatcher,
  71.         CartService                        $cartService,
  72.         KmWishlistGuestPageLoader $kMWishlistGuestPageLoader,
  73.         ProductOptionsAddToCart $productOptionsAddToCart
  74.     )
  75.     {
  76.         $this->wishlistPageLoader $wishlistPageLoader;
  77.         $this->wishlistLoadRoute $wishlistLoadRoute;
  78.         $this->addWishlistRoute $addWishlistRoute;
  79.         $this->removeWishlistProductRoute $removeWishlistProductRoute;
  80.         $this->mergeWishlistProductRoute $mergeWishlistProductRoute;
  81.         $this->guestPageLoader $guestPageLoader;
  82.         $this->guestPageletLoader $guestPageletLoader;
  83.         $this->eventDispatcher $eventDispatcher;
  84.         $this->cartService $cartService;
  85.         $this->kMWishlistGuestPageLoader $kMWishlistGuestPageLoader;
  86.         $this->productOptionsAddToCart $productOptionsAddToCart;
  87.     }
  88.     private function wishlistIndex(Request $requestSalesChannelContext $context){
  89.         /**
  90.          * @var \KmPitchPrint\Services\TicketService
  91.          *
  92.          * Synchronisiere den Ticketstatus mit freescout
  93.          */
  94.         $ticket $this->container->get('km_pitch_print.services.ticket');
  95.         $ticket->syncCustomersTicketStatus($request$context);
  96.         $customer $context->getCustomer();
  97.         if ($customer !== null) {
  98.             $page $this->wishlistPageLoader->load($request$context$customer);
  99.         } else {
  100.             $page $this->kMWishlistGuestPageLoader->load($request$context);
  101.         }
  102.         return $page;
  103.     }
  104.     /**
  105.      * @Since("6.3.4.0")
  106.      * @Route("/wishlist/cartoffcanvas", name="frontend.wishlist.page.offcanvas", options={"seo"="false"}, methods={"GET"})
  107.      */
  108.     public function index_cartoffcanvas(Request $requestSalesChannelContext $context): Response
  109.     {
  110.         $page $this->wishlistIndex($request$context);
  111.         if ($page->getWishlist()->getProductListing()->getTotal() < 1){
  112.             return $this->redirectToRoute('frontend.checkout.cart.page');
  113.         }else{
  114.             return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig',
  115.                 ['page' => $page,
  116.                 'offcanvas'=>True,
  117.                 ]);
  118.         }
  119.     }
  120.     /**
  121.      * @Since("6.3.4.0")
  122.      * @Route("/wishlist", name="frontend.wishlist.page", options={"seo"="false"}, methods={"GET"})
  123.      */
  124.     public function index(Request $requestSalesChannelContext $context): Response
  125.     {
  126.         $page $this->wishlistIndex($request$context);
  127.         return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
  128.     }
  129.     /**
  130.      * @Since("6.3.5.0")
  131.      * @Route("/wishlist/guest-pagelet", name="frontend.wishlist.guestPage.pagelet", options={"seo"="false"}, methods={"POST","GET"}, defaults={"XmlHttpRequest"=true})
  132.      */
  133.     public function guestPagelet(Request $requestSalesChannelContext $context): Response
  134.     {
  135.         $customer $context->getCustomer();
  136.         if ($customer !== null && $customer->getGuest() === false) {
  137.             throw new NotFoundHttpException();
  138.         }
  139.         $pagelet $this->guestPageletLoader->load($request$context);
  140.         return $this->renderStorefront(
  141.             '@Storefront/storefront/page/wishlist/wishlist-pagelet.html.twig',
  142.             ['page' => $pagelet'searchResult' => $pagelet->getSearchResult()->getObject()]
  143.         );
  144.     }
  145.     /**
  146.      * @Since("6.3.4.0")
  147.      * @LoginRequired()
  148.      * @Route("/widgets/wishlist", name="widgets.wishlist.pagelet", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  149.      */
  150.     public function ajaxPagination(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  151.     {
  152.         $request->request->set('no-aggregations'true);
  153.         $page $this->wishlistPageLoader->load($request$context$customer);
  154.         return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
  155.     }
  156.     /**
  157.      * @Since("6.3.4.0")
  158.      * @LoginRequired(allowGuest=true)
  159.      * @Route("/wishlist/list", name="frontend.wishlist.product.list", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  160.      */
  161.     public function ajaxList(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  162.     {
  163.         $criteria = new Criteria();
  164.         $this->eventDispatcher->dispatch(new WishListPageProductCriteriaEvent($criteria$context$request));
  165.         try {
  166.             $res $this->wishlistLoadRoute->load($request$context$criteria$customer);
  167.         } catch (CustomerWishlistNotFoundException $exception) {
  168.             return new JsonResponse([]);
  169.         }
  170.         $productWishlistIds $res->getProductListing()->getIds();
  171.         $ppRepo $this->container->get('km_product_pitchprint.repository');
  172.         $criteria = new Criteria();
  173.         $eqFilters = [];
  174.         $compPids = [];
  175.         foreach ($productWishlistIds as $pId) {
  176.             $eqFilters[] = new EqualsFilter('productId'$pId);
  177.             $compPids[] = $pId;
  178.         }
  179.         $criteria->addFilter(
  180.             new MultiFilter(
  181.                 MultiFilter::CONNECTION_OR,
  182.                 $eqFilters
  183.             )
  184.         );
  185.         $criteria->addFilter(new EqualsFilter('cookieId'$request->cookies->get('pp_id')));
  186.         $criteria->addFilter(new NotFilter(
  187.             NotFilter::CONNECTION_OR,
  188.             [
  189.                 new EqualsFilter('wishlistProductId'NULL)
  190.             ]
  191.         ));
  192.         $ppProductsCollection $ppRepo->search($criteria$context->getContext());
  193.         $ppIds = [];
  194.         //regenerate key-value with projectId-productId
  195.         foreach ($ppProductsCollection as $ppProduct) {
  196.             $ppIds[$ppProduct->getProjectId()] = $ppProduct->getProductId();
  197.             unset($productWishlistIds[$ppProduct->getProductId()]);
  198.         }
  199.         return new JsonResponse(array_merge($productWishlistIds$ppIds));
  200.     }
  201.     /**
  202.      * @Since("6.3.4.0")
  203.      * @LoginRequired()
  204.      * @Route("/wishlist/product/delete/{id}", name="frontend.wishlist.product.delete", methods={"POST", "DELETE"}, defaults={"XmlHttpRequest"=true})
  205.      */
  206.     public function remove(string $idRequest $requestSalesChannelContext $contextCustomerEntity $customer): Response
  207.     {
  208.         if (!$id) {
  209.             throw new MissingRequestParameterException('Parameter id missing');
  210.         }
  211.         try {
  212.             $this->removeWishlistProductRoute->delete($id$context$customer);
  213.             $this->addFlash(self::SUCCESS$this->trans('wishlist.itemDeleteSuccess'));
  214.         } catch (\Throwable $exception) {
  215.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  216.         }
  217.         return $this->createActionResponse($request);
  218.     }
  219.     /**
  220.      * @Since("6.3.4.0")
  221.      * @LoginRequired(allowGuest=true)
  222.      * @Route("/wishlist/pp-product/delete/{id}/{projectId}", name="frontend.wishlist.pp-product.delete", methods={"POST", "DELETE"}, defaults={"XmlHttpRequest"=true})
  223.      */
  224.     public function ppRemove(string $idstring $projectIdRequest $requestSalesChannelContext $contextCustomerEntity $customer): Response
  225.     {
  226.         if (!$id) {
  227.             throw new MissingRequestParameterException('Parameter id missing');
  228.         }
  229.         if (!$projectId) {
  230.             throw new MissingRequestParameterException('Parameter projectId missing');
  231.         }
  232.         $pp_repo $this->container->get('km_product_pitchprint.repository');
  233.         $criteria = new Criteria();
  234.         $criteria->addFilter(new EqualsFilter('projectId'$projectId));
  235.         $criteria->addFilter(new EqualsFilter('cookieId'$request->cookies->get('pp_id')));
  236.         $ppProject $pp_repo->search($criteria$context->getContext())->first();
  237.         //delete or. close open tickets
  238.         if($ppProject->getTicketId()>0){
  239.             /**
  240.              * @var \KmPitchPrint\Services\TicketService
  241.              */
  242.             $ticket $this->container->get('km_pitch_print.services.ticket');
  243.             $updData = [
  244.                 'status'=>'closed'
  245.             ];
  246.             $ticket->updateConversation($ppProject->getTicketId(),$updData);
  247.         }
  248.         $wishlistProductId $ppProject->getWishlistProductId();
  249.         $deleted $pp_repo->delete([
  250.             [
  251.                 'id' => $ppProject->getId()
  252.             ]
  253.         ], $context->getContext());
  254.         if ($deleted) {
  255.             $criteria = new Criteria();
  256.             #$criteria->addFilter(new EqualsFilter('productId', $ppProject->getProductId()));
  257.             #$criteria->addFilter(new EqualsFilter('cookieId', $request->cookies->get('pp_id')));
  258.             $criteria->addFilter(new EqualsFilter('wishlistProductId'$wishlistProductId));
  259.             $ppProject $pp_repo->search($criteria$context->getContext());
  260.             //delete the wishlistitem only if the pp-projects are not available in the repository
  261.             if ($ppProject->getTotal() > 0) {
  262.                 return $this->createActionResponse($request);
  263.             }
  264.         }
  265.         try {
  266.             $this->removeWishlistProductRoute->delete($id$context$customer);
  267.             $this->addFlash(self::SUCCESS$this->trans('wishlist.itemDeleteSuccess'));
  268.         } catch (\Throwable $exception) {
  269.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  270.         }
  271.         return $this->createActionResponse($request);
  272.     }
  273.     /**
  274.      * @Since("6.3.4.0")
  275.      * @LoginRequired()
  276.      * @Route("/wishlist/add/{productId}/{lineItemId}", name="frontend.wishlist.product.checkout-add", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  277.      */
  278.     public function ajaxCheckoutAdd(string $productIdstring $lineItemIdSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  279.     {
  280.         $this->addWishlistRoute->add($productId$context$customer);
  281.         /**
  282.          * @var Cart
  283.          */
  284.         $cart $this->cartService->getCart($context->getToken(), $context);
  285.         $this->cartService->remove($cart$lineItemId$context);
  286.         return new JsonResponse([
  287.             'success' => true,
  288.         ]);
  289.     }
  290.     private function addWishlist(string $productIdSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  291.     {
  292.         $this->addWishlistRoute->add($productId$context$customer);
  293.         return new JsonResponse([
  294.             'success' => true,
  295.         ]);
  296.     }
  297.     /**
  298.      * @Since("6.3.4.0")
  299.      * @LoginRequired()
  300.      * @Route("/wishlist/add/{productId}", name="frontend.wishlist.product.add", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  301.      */
  302.     public function ajaxAdd(string $productIdRequest $requestRequestDataBag $requestDataBagSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  303.     {
  304.         if (!$request->cookies->get('pp_id')) {
  305.             $rand_hex Uuid::randomHex();
  306.             $cookie Cookie::create('pp_id'$rand_hextime() + (365 24 60 60));
  307.             $response = new Response();
  308.             $response->headers->setCookie($cookie);
  309.             $response->sendHeaders();
  310.         }
  311.         if ($customer !== null && $customer->getGuest() === false) {
  312.             $payload = [
  313.                 'qty' => 1,
  314.                 'swoptions' => [],
  315.                 'type' => 'product',
  316.             ];
  317.             if($request->get('qty')){
  318.                 $payload['qty'] = (int)$request->get('qty');
  319.             }
  320.             $this->eventDispatcher->dispatch(new MoveToWishlistEvent($request$requestDataBag, (string)$payload['qty'], $productId$payload$context));
  321.         }
  322.         return new JsonResponse();
  323.     }
  324.     /**
  325.      * @Since("6.3.4.0")
  326.      * @LoginRequired()
  327.      * @Route("/wishlist/add/{productId}/{projectId}/{lineItemId}", name="frontend.wishlist.product.checkout-add-pp", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  328.      */
  329.     public function ajaxPPAdd(string $productIdstring $projectIdstring $lineItemIdRequest $requestRequestDataBag $requestDataBagSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  330.     {
  331.         $this->eventDispatcher->dispatch(new MoveToWishlistEvent($request$requestDataBag, (string)$request->get('qty'), $productId, [], $context));
  332.         $cart $this->cartService->getCart($context->getToken(), $context);
  333.         $this->cartService->remove($cart$lineItemId$context);
  334.         return new JsonResponse([
  335.             'pp' => true,
  336.         ]);
  337.     }
  338.     /**
  339.      * @Since("6.3.4.0")
  340.      * @LoginRequired()
  341.      * @Route("/wishlist/remove/{productId}", name="frontend.wishlist.product.remove", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  342.      */
  343.     public function ajaxRemove(string $productIdSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  344.     {
  345.         $this->removeWishlistProductRoute->delete($productId$context$customer);
  346.         return new JsonResponse([
  347.             'success' => true,
  348.         ]);
  349.     }
  350.     /**
  351.      * @Since("6.3.4.0")
  352.      * @LoginRequired()
  353.      * @Route("/wishlist/add-after-login/{productId}", name="frontend.wishlist.add.after.login", options={"seo"="false"}, methods={"GET"})
  354.      */
  355.     public function addAfterLogin(string $productIdRequest $requestRequestDataBag $requestDataBagSalesChannelContext $contextCustomerEntity $customer): Response
  356.     {
  357.         //string $productId,Request $request, SalesChannelContext $context, CustomerEntity $customer
  358.         $this->ajaxAdd($productId$request$requestDataBag$context$context->getCustomer());
  359.         return $this->redirectToRoute('frontend.home.page');
  360.         /*try {
  361.             $this->addWishlistRoute->add($productId, $context, $customer);
  362.             $this->addFlash(self::SUCCESS, $this->trans('wishlist.itemAddedSuccess'));
  363.         } catch (DuplicateWishlistProductException $exception) {
  364.             $this->addFlash(self::WARNING, $exception->getMessage());
  365.         } catch (\Throwable $exception) {
  366.             $this->addFlash(self::DANGER, $this->trans('error.message-default'));
  367.         }
  368.         return $this->redirectToRoute('frontend.home.page');*/
  369.     }
  370.     /**
  371.      * @Since("6.3.4.0")
  372.      * @LoginRequired()
  373.      * @Route("/wishlist/merge", name="frontend.wishlist.product.merge", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  374.      */
  375.     public function ajaxMerge(RequestDataBag $requestDataBagRequest $requestSalesChannelContext $contextCustomerEntity $customer): Response
  376.     {
  377.         try {
  378.             $this->mergeWishlistProductRoute->merge($requestDataBag$context$customer);
  379.             return $this->renderStorefront('@Storefront/storefront/utilities/alert.html.twig', [
  380.                 'type' => 'info''content' => $this->trans('wishlist.wishlistMergeHint'),
  381.             ]);
  382.         } catch (\Throwable $exception) {
  383.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  384.         }
  385.         return $this->createActionResponse($request);
  386.     }
  387.     /**
  388.      * @Since("6.3.4.0")
  389.      * @LoginRequired()
  390.      * @Route("/wishlist/merge/pagelet", name="frontend.wishlist.product.merge.pagelet", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  391.      */
  392.     public function ajaxPagelet(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  393.     {
  394.         $request->request->set('no-aggregations'true);
  395.         $page $this->wishlistPageLoader->load($request$context$customer);
  396.         return $this->renderStorefront('@Storefront/storefront/page/wishlist/wishlist-pagelet.html.twig', ['page' => $page]);
  397.     }
  398. }