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.  * @RouteScope(scopes={"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.      * @LoginRequired()
  101.      * @Route("/widgets/wishlist", name="widgets.wishlist.pagelet", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  102.      */
  103.     public function ajaxPagination(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  104.     {
  105.         $request->request->set('no-aggregations'true);
  106.         $page $this->wishlistPageLoader->load($request$context$customer);
  107.         $this->hook(new WishlistPageLoadedHook($page$context));
  108.         $response $this->renderStorefront('@Storefront/storefront/page/wishlist/index.html.twig', ['page' => $page]);
  109.         $response->headers->set('x-robots-tag''noindex');
  110.         return $response;
  111.     }
  112.     /**
  113.      * @Since("6.3.4.0")
  114.      * @LoginRequired()
  115.      * @Route("/wishlist/list", name="frontend.wishlist.product.list", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  116.      */
  117.     public function ajaxList(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  118.     {
  119.         $criteria = new Criteria();
  120.         $this->eventDispatcher->dispatch(new WishListPageProductCriteriaEvent($criteria$context$request));
  121.         try {
  122.             $res $this->wishlistLoadRoute->load($request$context$criteria$customer);
  123.         } catch (CustomerWishlistNotFoundException $exception) {
  124.             return new JsonResponse([]);
  125.         }
  126.         return new JsonResponse($res->getProductListing()->getIds());
  127.     }
  128.     /**
  129.      * @Since("6.3.4.0")
  130.      * @LoginRequired()
  131.      * @Route("/wishlist/product/delete/{id}", name="frontend.wishlist.product.delete", methods={"POST", "DELETE"}, defaults={"XmlHttpRequest"=true})
  132.      */
  133.     public function remove(string $idRequest $requestSalesChannelContext $contextCustomerEntity $customer): Response
  134.     {
  135.         if (!$id) {
  136.             throw new MissingRequestParameterException('Parameter id missing');
  137.         }
  138.         try {
  139.             $this->removeWishlistProductRoute->delete($id$context$customer);
  140.             $this->addFlash(self::SUCCESS$this->trans('wishlist.itemDeleteSuccess'));
  141.         } catch (\Throwable $exception) {
  142.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  143.         }
  144.         return $this->createActionResponse($request);
  145.     }
  146.     /**
  147.      * @Since("6.3.4.0")
  148.      * @LoginRequired()
  149.      * @Route("/wishlist/add/{productId}", name="frontend.wishlist.product.add", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  150.      */
  151.     public function ajaxAdd(string $productIdSalesChannelContext $contextCustomerEntity $customer): JsonResponse
  152.     {
  153.         try {
  154.             $this->addWishlistRoute->add($productId$context$customer);
  155.             $success true;
  156.         } catch (\Throwable $exception) {
  157.             $success false;
  158.         }
  159.         return new JsonResponse([
  160.             'success' => $success,
  161.         ]);
  162.     }
  163.     /**
  164.      * @Since("6.3.4.0")
  165.      * @LoginRequired()
  166.      * @Route("/wishlist/remove/{productId}", name="frontend.wishlist.product.remove", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=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.      * @LoginRequired()
  183.      * @Route("/wishlist/add-after-login/{productId}", name="frontend.wishlist.add.after.login", options={"seo"="false"}, methods={"GET"})
  184.      */
  185.     public function addAfterLogin(string $productIdSalesChannelContext $contextCustomerEntity $customer): Response
  186.     {
  187.         try {
  188.             $this->addWishlistRoute->add($productId$context$customer);
  189.             $this->addFlash(self::SUCCESS$this->trans('wishlist.itemAddedSuccess'));
  190.         } catch (DuplicateWishlistProductException $exception) {
  191.             $this->addFlash(self::WARNING$this->trans('wishlist.duplicateItemError'));
  192.         } catch (\Throwable $exception) {
  193.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  194.         }
  195.         return $this->redirectToRoute('frontend.home.page');
  196.     }
  197.     /**
  198.      * @Since("6.3.4.0")
  199.      * @LoginRequired()
  200.      * @Route("/wishlist/merge", name="frontend.wishlist.product.merge", options={"seo"="false"}, methods={"POST"}, defaults={"XmlHttpRequest"=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.      * @LoginRequired()
  217.      * @Route("/wishlist/merge/pagelet", name="frontend.wishlist.product.merge.pagelet", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  218.      */
  219.     public function ajaxPagelet(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  220.     {
  221.         $request->request->set('no-aggregations'true);
  222.         $page $this->wishlistPageLoader->load($request$context$customer);
  223.         $this->hook(new WishlistWidgetLoadedHook($page$context));
  224.         return $this->renderStorefront('@Storefront/storefront/page/wishlist/wishlist-pagelet.html.twig', [
  225.             'page' => $page,
  226.             'searchResult' => $page->getWishlist()->getProductListing(),
  227.         ]);
  228.     }
  229. }