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

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