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

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