vendor/shopware/storefront/Controller/WishlistController.php line 98

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Customer\CustomerEntity;
  4. use Shopware\Core\Checkout\Customer\Exception\CustomerWishlistNotFoundException;
  5. use Shopware\Core\Checkout\Customer\Exception\DuplicateWishlistProductException;
  6. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractAddWishlistProductRoute;
  7. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLoadWishlistRoute;
  8. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractMergeWishlistProductRoute;
  9. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractRemoveWishlistProductRoute;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  12. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  13. use Shopware\Core\Framework\Routing\Annotation\Since;
  14. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  15. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  18. use Shopware\Storefront\Page\Wishlist\GuestWishlistPageLoadedHook;
  19. use Shopware\Storefront\Page\Wishlist\GuestWishlistPageLoader;
  20. use Shopware\Storefront\Page\Wishlist\WishlistPageLoadedHook;
  21. use Shopware\Storefront\Page\Wishlist\WishlistPageLoader;
  22. use Shopware\Storefront\Page\Wishlist\WishListPageProductCriteriaEvent;
  23. use Shopware\Storefront\Page\Wishlist\WishlistWidgetLoadedHook;
  24. use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoadedHook;
  25. use Shopware\Storefront\Pagelet\Wishlist\GuestWishlistPageletLoader;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpFoundation\Response;
  29. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  32. /**
  33.  * @Route(defaults={"_routeScope"={"storefront"}})
  34.  *
  35.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  36.  */
  37. class WishlistController extends StorefrontController
  38. {
  39.     private WishlistPageLoader $wishlistPageLoader;
  40.     private AbstractLoadWishlistRoute $wishlistLoadRoute;
  41.     private AbstractAddWishlistProductRoute $addWishlistRoute;
  42.     private AbstractRemoveWishlistProductRoute $removeWishlistProductRoute;
  43.     private AbstractMergeWishlistProductRoute $mergeWishlistProductRoute;
  44.     private GuestWishlistPageLoader $guestPageLoader;
  45.     private GuestWishlistPageletLoader $guestPageletLoader;
  46.     private EventDispatcherInterface $eventDispatcher;
  47.     /**
  48.      * @internal
  49.      */
  50.     public function __construct(
  51.         WishlistPageLoader $wishlistPageLoader,
  52.         AbstractLoadWishlistRoute $wishlistLoadRoute,
  53.         AbstractAddWishlistProductRoute $addWishlistRoute,
  54.         AbstractRemoveWishlistProductRoute $removeWishlistProductRoute,
  55.         AbstractMergeWishlistProductRoute $mergeWishlistProductRoute,
  56.         GuestWishlistPageLoader $guestPageLoader,
  57.         GuestWishlistPageletLoader $guestPageletLoader,
  58.         EventDispatcherInterface $eventDispatcher
  59.     ) {
  60.         $this->wishlistPageLoader $wishlistPageLoader;
  61.         $this->wishlistLoadRoute $wishlistLoadRoute;
  62.         $this->addWishlistRoute $addWishlistRoute;
  63.         $this->removeWishlistProductRoute $removeWishlistProductRoute;
  64.         $this->mergeWishlistProductRoute $mergeWishlistProductRoute;
  65.         $this->guestPageLoader $guestPageLoader;
  66.         $this->guestPageletLoader $guestPageletLoader;
  67.         $this->eventDispatcher $eventDispatcher;
  68.     }
  69.     /**
  70.      * @Since("6.3.4.0")
  71.      * @Route("/wishlist", name="frontend.wishlist.page", options={"seo"="false"}, methods={"GET"})
  72.      * @NoStore
  73.      */
  74.     public function index(Request $requestSalesChannelContext $context): Response
  75.     {
  76.         $customer $context->getCustomer();
  77.         if ($customer !== null && $customer->getGuest() === false) {
  78.             $page $this->wishlistPageLoader->load($request$context$customer);
  79.             $this->hook(new WishlistPageLoadedHook($page$context));
  80.         } else {
  81.             $page $this->guestPageLoader->load($request$context);
  82.             $this->hook(new GuestWishlistPageLoadedHook($page$context));
  83.         }
  84.         return $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
  85.     }
  86.     /**
  87.      * @Since("6.3.5.0")
  88.      * @Route("/wishlist/guest-pagelet", name="frontend.wishlist.guestPage.pagelet", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  89.      */
  90.     public function guestPagelet(Request $requestSalesChannelContext $context): Response
  91.     {
  92.         $customer $context->getCustomer();
  93.         if ($customer !== null && $customer->getGuest() === false) {
  94.             throw new NotFoundHttpException();
  95.         }
  96.         $pagelet $this->guestPageletLoader->load($request$context);
  97.         $this->hook(new GuestWishlistPageletLoadedHook($pagelet$context));
  98.         return $this->renderStorefront(
  99.             '@Storefront/storefront/page/wishlist/wishlist-pagelet.html.twig',
  100.             ['page' => $pagelet'searchResult' => $pagelet->getSearchResult()->getObject()]
  101.         );
  102.     }
  103.     /**
  104.      * @Since("6.3.4.0")
  105.      * @Route("/widgets/wishlist", name="widgets.wishlist.pagelet", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  106.      */
  107.     public function ajaxPagination(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  108.     {
  109.         $request->request->set('no-aggregations'true);
  110.         $page $this->wishlistPageLoader->load($request$context$customer);
  111.         $this->hook(new WishlistPageLoadedHook($page$context));
  112.         $response $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
  113.         $response->headers->set('x-robots-tag''noindex');
  114.         return $response;
  115.     }
  116.     /**
  117.      * @Since("6.3.4.0")
  118.      * @Route("/wishlist/list", name="frontend.wishlist.product.list", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  119.      */
  120.     public function ajaxList(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  121.     {
  122.         $criteria = new Criteria();
  123.         $this->eventDispatcher->dispatch(new WishListPageProductCriteriaEvent($criteria$context$request));
  124.         try {
  125.             $res $this->wishlistLoadRoute->load($request$context$criteria$customer);
  126.         } catch (CustomerWishlistNotFoundException $exception) {
  127.             return new JsonResponse([]);
  128.         }
  129.         return new JsonResponse($res->getProductListing()->getIds());
  130.     }
  131.     /**
  132.      * @Since("6.3.4.0")
  133.      * @Route("/wishlist/product/delete/{id}", name="frontend.wishlist.product.delete", methods={"POST", "DELETE"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  134.      */
  135.     public function remove(string $idRequest $requestSalesChannelContext $contextCustomerEntity $customer): Response
  136.     {
  137.         if (!$id) {
  138.             throw new MissingRequestParameterException('Parameter id missing');
  139.         }
  140.         try {
  141.             $this->removeWishlistProductRoute->delete($id$context$customer);
  142.             $this->addFlash(self::SUCCESS$this->trans('wishlist.itemDeleteSuccess'));
  143.         } catch (\Throwable $exception) {
  144.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  145.         }
  146.         return $this->createActionResponse($request);
  147.     }
  148.     /**
  149.      * @Since("6.3.4.0")
  150.      * @Route("/wishlist/add/{productId}", name="frontend.wishlist.product.add", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  151.      */
  152.     public function ajaxAdd(string $productIdSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  153.     {
  154.         try {
  155.             $this->addWishlistRoute->add($productId$context$customer);
  156.             $success true;
  157.         } catch (\Throwable $exception) {
  158.             $success false;
  159.         }
  160.         return new JsonResponse([
  161.             'success' => $success,
  162.         ]);
  163.     }
  164.     /**
  165.      * @Since("6.3.4.0")
  166.      * @Route("/wishlist/remove/{productId}", name="frontend.wishlist.product.remove", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  167.      */
  168.     public function ajaxRemove(string $productIdSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  169.     {
  170.         try {
  171.             $this->removeWishlistProductRoute->delete($productId$context$customer);
  172.             $success true;
  173.         } catch (\Throwable $exception) {
  174.             $success false;
  175.         }
  176.         return new JsonResponse([
  177.             'success' => $success,
  178.         ]);
  179.     }
  180.     /**
  181.      * @Since("6.3.4.0")
  182.      * @Route("/wishlist/add-after-login/{productId}", name="frontend.wishlist.add.after.login", options={"seo"="false"}, methods={"GET"}, defaults={"_loginRequired"=true})
  183.      */
  184.     public function addAfterLogin(string $productIdSalesChannelContext $contextCustomerEntity $customer): Response
  185.     {
  186.         try {
  187.             $this->addWishlistRoute->add($productId$context$customer);
  188.             $this->addFlash(self::SUCCESS$this->trans('wishlist.itemAddedSuccess'));
  189.         } catch (DuplicateWishlistProductException $exception) {
  190.             $this->addFlash(self::WARNING$this->trans('wishlist.duplicateItemError'));
  191.         } catch (\Throwable $exception) {
  192.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  193.         }
  194.         return $this->redirectToRoute('frontend.home.page');
  195.     }
  196.     /**
  197.      * @Since("6.3.4.0")
  198.      * @Route("/wishlist/merge", name="frontend.wishlist.product.merge", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  199.      */
  200.     public function ajaxMerge(RequestDataBag $requestDataBagRequest $requestSalesChannelContext $contextCustomerEntity $customer): Response
  201.     {
  202.         try {
  203.             $this->mergeWishlistProductRoute->merge($requestDataBag$context$customer);
  204.             return $this->renderStorefront('@Storefront/storefront/utilities/alert.html.twig', [
  205.                 'type' => 'info''content' => $this->trans('wishlist.wishlistMergeHint'),
  206.             ]);
  207.         } catch (\Throwable $exception) {
  208.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  209.         }
  210.         return $this->createActionResponse($request);
  211.     }
  212.     /**
  213.      * @Since("6.3.4.0")
  214.      * @Route("/wishlist/merge/pagelet", name="frontend.wishlist.product.merge.pagelet", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  215.      */
  216.     public function ajaxPagelet(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  217.     {
  218.         $request->request->set('no-aggregations'true);
  219.         $page $this->wishlistPageLoader->load($request$context$customer);
  220.         $this->hook(new WishlistWidgetLoadedHook($page$context));
  221.         return $this->renderStorefront('@Storefront/storefront/page/wishlist/wishlist-pagelet.html.twig', [
  222.             'page' => $page,
  223.             'searchResult' => $page->getWishlist()->getProductListing(),
  224.         ]);
  225.     }
  226. }