<?php declare(strict_types=1);
/**
* PremSoft
* Copyright © 2019 Premsoft - Sven Mittreiter
*
* @copyright Copyright (c) 2019, premsoft - Sven Mittreiter (http://www.premsoft.de)
* @author Sven Mittreiter <info@premsoft.de>
*/
namespace Prems\Plugin\PremsWishlist\Storefront\Controller;
use Prems\Plugin\PremsWishlist\Core\Wishlist\Storefront\ConfigService;
use Prems\Plugin\PremsWishlist\Entity\Wishlist\Aggregate\WishlistProduct\WishlistProductEntity;
use Prems\Plugin\PremsWishlist\Exception\Wishlist\WishlistIdMissingException;
use Prems\Plugin\PremsWishlist\Exception\Wishlist\WishlistNotFoundException;
use Shopware\Core\Checkout\Cart\Exception\CartTokenNotFoundException;
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
use Shopware\Core\Checkout\Cart\Exception\InvalidPayloadException;
use Shopware\Core\Checkout\Cart\Exception\InvalidQuantityException;
use Shopware\Core\Checkout\Cart\Exception\MixedLineItemTypeException;
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\PlatformRequest;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Storefront\Page\GenericPageLoaderInterface;
use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Prems\Plugin\PremsWishlist\Core\Wishlist\Storefront\WishlistService;
use Prems\Plugin\PremsWishlist\Core\Wishlist\Storefront\ProductService;
use Prems\Plugin\PremsWishlist\Storefront\Page\Detail\DetailPageLoader;
use Prems\Plugin\PremsWishlist\Storefront\Page\Listing\ListingPageLoader;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* @RouteScope(scopes={"storefront"})
*/
class WishlistController extends StorefrontController
{
private GenericPageLoaderInterface $genericPageLoader;
/**
* @var DetailPageLoader
*/
private $detailPageLoader;
/**
* @var ListingPageLoader
*/
private $listingPageLoader;
/**
* @var WishlistService
*/
private $wishlistService;
/**
* @var ProductService
*/
private $productService;
/**
* @var ConfigService
*/
private $configService;
/**
* @var Session
*/
private $sessionService;
/**
* @var \Shopware\Core\Framework\Struct\Struct
*/
protected $wishlistSettings;
public function __construct(
GenericPageLoaderInterface $genericPageLoader,
ListingPageLoader $listingPageLoader,
DetailPageLoader $detailPageLoader,
WishlistService $wishlistService,
ProductService $productService,
ConfigService $configService
) {
$this->genericPageLoader = $genericPageLoader;
$this->listingPageLoader = $listingPageLoader;
$this->detailPageLoader = $detailPageLoader;
$this->wishlistService = $wishlistService;
$this->productService = $productService;
$this->sessionService = new Session();
$this->configService = $configService;
}
/**
* Get plugin settings
*
* @throws \Exception
* @return \Shopware\Core\Framework\Struct\Struct
*/
protected function getWishlistSettings() {
if ($this->wishlistSettings) {
return $this->wishlistSettings;
}
$this->wishlistSettings = $this->configService->getConfig();
if (!$this->wishlistSettings) {
throw new \Exception('No wishlist settings found');
}
return $this->wishlistSettings;
}
/**
* Shows all wishlist of a customer. If not logged in then redirected to login page
* @Route(
* "/wishlist/index",
* name="frontend.PremsWishlist.index",
* methods={"GET"}
* )
*
* @param SalesChannelContext $context
* @param Request $request
*
* @return Response
*/
public function index(SalesChannelContext $context, Request $request): Response
{
$maxWishlistNumberReached = false;
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
if (!$context->getCustomer()) {
$this->addFlash('info', $this->trans('prems-wishlist.status_messages.login_recommended'));
}
$wishlistId = $request->request->get('wishlistId', 0);
$premsWishlistStoredArticle = $this->sessionService->get('premsWishlistStoredArticle');
$page = $this->listingPageLoader->load($request, $context);
$maxNumberWishlistsPerUser = (int) $this->getWishlistSettings()->getMaxNumberWishlistsPerUser();
if ($maxNumberWishlistsPerUser == 0 || $page->getListing()->getTotal() < $maxNumberWishlistsPerUser) {
$maxWishlistNumberReached = false;
} else {
$maxWishlistNumberReached = true;
$this->addFlash('info', $this->trans('prems-wishlist.status_messages.max_number_wishlists_per_user_reached'));
}
$response = $this->renderStorefront('@PremsWishlist/storefront/page/wishlist/list.html.twig', [
'maxWishlistNumberReached' => $maxWishlistNumberReached,
'premsWishlist' => $this->getWishlistSettings(),
'page' => $page,
'storedArticle' => $premsWishlistStoredArticle,
'wishlistId' => $wishlistId
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* @Route(
* "/widgets/wishlist/detail/{id}",
* name="frontend.PremsWishlist.ajaxWishlistDetail",
* options={"seo"="false"},
* methods={"GET"},
* defaults={"XmlHttpRequest"=true})
*
* @param SalesChannelContext $context
* @param Request $request
*/
public function ajaxWishlistDetail(SalesChannelContext $context, Request $request)
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$wishlistId = (string) $request->attributes->get('id');
if ($wishlistId === '') {
throw new MissingRequestParameterException('id');
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context, true);
if (!$wishlist) {
throw new WishlistNotFoundException("Wishlist $wishlist not found");
}
$customerIsOwner = false;
$customer = $context->getCustomer();
if ($customer) {
$customerId = $context->getCustomer()->getId();
if ($customerId === $wishlist->getCustomer()->getId()) {
$customerIsOwner = true;
}
} else {
$token = $context->getToken();
if ($token === $wishlist->getToken()) {
$customerIsOwner = true;
}
}
$isPublic = !$wishlist->isPrivate();
if (! ($isPublic || $customerIsOwner)) {
throw new AccessDeniedException($wishlistId);
}
$response = $this->renderStorefront('@PremsWishlist/storefront/page/wishlist/wishlist-detail-list.html.twig',
[
'premsWishlist' => $this->getWishlistSettings(),
'wishlistId' => $wishlistId,
'wishlist' => $wishlist,
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* Shows number of articles in all wishlist of user. Used for count sign in header
* @Route(
* "/wishlist/count",
* name="frontend.PremsWishlist.count",
* methods={"GET"}
* )
*
* @param SalesChannelContext $context
* @param Request $request
*
* @return JsonResponse
*/
public function count(SalesChannelContext $context, Request $request): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired() && !$context->getCustomer()) {
$response = new JsonResponse([
'success' => true,
'count' => 0,
'products' => []
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
$results = $this->wishlistService->getProductCountForUser($context);
/**$wishlistSettings = null;
$wishlistSettings = $this->configService->getConfig();
if (!$wishlistSettings) {
throw new \Exception('No wishlist settings found');
}
$premsWishlistStoredArticle = $this->sessionService->get($request, 'premsWishlistStoredArticle');
$page = $this->listingPageLoader->load($request, $context);
return $this->renderStorefront('@PremsWishlist/storefront/page/wishlist/index.html.twig', [
'premsWishlist' => $wishlistSettings,
'page' => $page,
'storedArticle' => $premsWishlistStoredArticle
]);*/
$response = new JsonResponse([
'success' => true,
'count' => $results['number'],
'products' => $results['products']
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* @Route(
* "/wishlist/{wishlistId}",
* name="frontend.PremsWishlist.detail",
* methods={"GET"}
* )
*
* @param SalesChannelContext $context
* @param Request $request
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function detail(SalesChannelContext $context, Request $request): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
try {
$page = $this->detailPageLoader->load($request, $context);
} catch (WishlistNotFoundException $e) {
$this->addFlash('warning', $this->trans('prems-wishlist.status_messages.access_denied'));
return $this->redirectToRoute('frontend.PremsWishlist.index');
} catch (WishlistIdMissingException $e) {
$this->addFlash('warning', $this->trans('prems-wishlist.status_messages.access_denied'));
return $this->redirectToRoute('frontend.PremsWishlist.index');
} catch (AccessDeniedException $e) {
$this->addFlash('warning', $this->trans('prems-wishlist.status_messages.access_denied'));
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
$customer = $context->getCustomer();
if ($this->getWishlistSettings()->isLoginRequired() && !$customer) {
$isLoggedIn = false;
} else {
$isLoggedIn = true;
}
$response = $this->renderStorefront('@PremsWishlist/storefront/page/wishlist/detail.html.twig', [
'isLoggedIn' => $isLoggedIn,
'premsWishlist' => $this->getWishlistSettings(),
'page' => $page
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* Modal window for adding product to wishlists
*
* @Route(
* "/wishlist/modal/{productId}",
* name="frontend.PremsWishlist.add.modal",
* options={"seo"="false"},
* methods={"GET"}
* )
*
* @param string $productId
* @param SalesChannelContext $context
* @param Request $request
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
*/
public function modalForAddingProduct(string $productId, SalesChannelContext $context, Request $request): Response
{
$maxWishlistNumberReached = false;
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
$customer = $context->getCustomer();
$product = $this->productService->getProductById($productId, $context);
$lists = [];
if ($this->getWishlistSettings()->isLoginRequired() && !$customer) {
$isLoggedIn = false;
$this->sessionService->set('premsWishlistStoredArticle', $productId);
} else {
$isLoggedIn = true;
$lists = $this->wishlistService->getWishlistsForUser($context);
$maxNumberWishlistsPerUser = (int) $this->getWishlistSettings()->getMaxNumberWishlistsPerUser();
if ($maxNumberWishlistsPerUser == 0 || count($lists) < $maxNumberWishlistsPerUser) {
$maxWishlistNumberReached = false;
} else {
$maxWishlistNumberReached = true;
$this->addFlash('info', $this->trans('prems-wishlist.status_messages.max_number_wishlists_per_user_reached'));
}
}
$response = $this->renderStorefront('@PremsWishlist/storefront/page/wishlist/modal_for_adding_product.html.twig', [
'maxWishlistNumberReached' => $maxWishlistNumberReached,
'loggedIn' => $isLoggedIn,
'lists' => $lists,
'product' => $product,
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* Update a wishlist
*
* @Route(
* "/wishlist/updateWishlist",
* name="frontend.PremsWishlist.updateWishlist",
* options={"seo"="false"}, methods={"POST"}
* )
*
* @param string $productId
* @param Request $request
* @param SalesChannelContext $context
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
*/
public function updateWishlist(Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$id = $request->request->get('id', '');
$listName = $request->request->get('listName', '');
$visibility = $request->request->get('visibility', 0);
if ($visibility) {
$private = false;
} else {
$private = true;
}
if (!$id) {
throw new WishlistNotFoundException($id);
}
$wishlist = $this->wishlistService->getWishlistById($id, $context);
if (!$wishlist) {
throw new WishlistNotFoundException($id);
}
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context, true)) {
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.update_failed'));
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
if ($listName) {
$this->wishlistService->updateWishlist([
[
'id' => $id,
'name' => $listName,
'private' => $private,
'salesChannelId' => $context->getSalesChannelId(),
]
], $context->getContext());
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.update_successful'));
} else {
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.update_failed'));
}
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
/**
* Create a new wishlist
*
* @Route(
* "/wishlist/createWishlist",
* name="frontend.PremsWishlist.createWishlist",
* options={"seo"="false"}, methods={"POST"}
* )
*
* @param string $productId
* @param Request $request
* @param SalesChannelContext $context
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
*/
public function createWishlist(Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$listName = $request->request->get('listName', '');
$user = $context->getCustomer();
$visibility = $request->request->get('visibility', 0);
if ($visibility) {
$private = false;
} else {
$private = true;
}
if ($listName) {
$wishlistData = [
'name' => $listName,
'private' => $private,
'priority' => 0,
];
if ($user) {
$wishlistData['customerId'] = $user->getId();
} else {
$wishlistData['token'] = $context->getToken();
}
$wishlistData['salesChannelId'] = $context->getSalesChannelId();
$this->wishlistService->createWishlist([
$wishlistData
], $context->getContext());
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.create_successful'));
} else {
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.create_failed'));
}
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
/**
* Saves an external public wishlist as an own private one
*
* @Route(
* "/wishlist/saveAsOwnWishlist",
* name="frontend.PremsWishlist.saveAsOwnWishlist",
* options={"seo"="false"}, methods={"POST"}
* )
*
* @param Request $request
* @param SalesChannelContext $context
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
*/
public function saveAsOwnWishlist(Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$wishlistId = $request->request->getAlnum('wishlistId');
if (!$wishlistId) {
throw new MissingRequestParameterException('wishlistId');
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context, true);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context)) {
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
$user = $context->getCustomer();
$sourceProducts = $wishlist->getProducts();
$targetProducts = [];
/** @var WishlistProductEntity $product */
foreach ($sourceProducts->getElements() as $product) {
$targetProducts[] = [
'productId' => $product->getProductId(),
'number_wished' => $product->getNumberWished(),
'position' => $product->getPosition(),
'hint' => $product->getHint(),
];
}
$wishlistData = [
'name' => $wishlist->getName(),
'private' => true,
'products' => $targetProducts,
'priority' => 0,
];
if ($user) {
$wishlistData['customerId'] = $user->getId();
} else {
$wishlistData['token'] = $context->getToken();
}
$wishlistData['salesChannelId'] = $context->getSalesChannelId();
$this->wishlistService->createWishlist([
$wishlistData
], $context->getContext());
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.create_successful'));
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
/**
* Adds a product by product number to wishlist
* @Route("/wishlist/add-by-number",
* name="frontend.PremsWishlist.addByProductNumber", methods={"POST"})
*
* @throws InconsistentCriteriaIdsException
* @throws MissingRequestParameterException
*/
public function addByProductNumber(Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$number = $request->request->get('number');
if (!$number) {
throw new MissingRequestParameterException('number');
}
$wishlistId = $request->request->getAlnum('wishlistId');
if (!$wishlistId) {
throw new MissingRequestParameterException('wishlistId');
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context)) {
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
$data = $this->productService->getProductByNumber($number, $context->getContext());
if (empty($data)) {
$this->addFlash('danger', $this->trans('error.productNotFound', ['%number%' => $number]));
return $this->redirectToRoute('frontend.PremsWishlist.index', ['wishlistId' => $wishlistId]);
}
$productId = array_shift($data);
$listIds[] = $wishlistId;
$lists = $this->wishlistService->getWishlistsByIds($listIds, $context);
// Add Article to List
$this->wishlistService->addProductToWishlists($productId, $lists, $context->getContext());
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.article_added_successful'));
return $this->redirectToRoute('frontend.PremsWishlist.index', ['wishlistId' => $wishlistId]);
}
/**
* Adds a product to a wishlist
*
* @Route(
* "/wishlist/add/{productId}/{wishlistId}",
* name="frontend.PremsWishlist.add",
* options={"seo"="false"}, methods={"GET"}
* )
*
* @param string $productId
* @param Request $request
* @param SalesChannelContext $context
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
*/
public function add(string $productId, string $wishlistId, Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
$lists = [];
$listIds = [];
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context)) {
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
// Add request is for a stored article? Then delete this session entry
if ($request->request->get('storedArticle') == 1) {
$this->sessionService->remove('premsWishlistStoredArticle');
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.article_added_successful'));
}
$user = $context->getCustomer();
$listIds[] = $wishlistId;
// ListIds given?
if (!empty($listIds)) {
$lists = $this->wishlistService->getWishlistsByIds($listIds, $context);
// Add Article to List
$this->wishlistService->addProductToWishlists($productId, $lists, $context->getContext());
}
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
/**
* Save all cart items as a new wishlist
*
* @Route(
* "/wishlist/addCartAsWishlist",
* name="frontend.PremsWishlist.add.cart.as.wishlist",
* options={"seo"="false"}, methods={"POST"}
* )
*
* @param string $productId
* @param Request $request
* @param SalesChannelContext $context
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
*/
public function saveCartAsWishlist(Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$listName = $request->request->get('listName', '');
if (strlen($listName) > 0) {
$this->wishlistService->saveCartAsWishlist(
$listName,
$context,
$this->getWishlistSettings()->isClearCartAfterAddingToWishlist()
);
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.create_successful'));
} else {
$this->addFlash('warning', $this->trans('prems-wishlist.status_messages.create_failed'));
}
return $this->redirectToRoute('frontend.checkout.cart.page');
}
/**
* Adds a product to a wishlist in modal window context
*
* @Route(
* "/wishlist/modalAdd/{productId}",
* name="frontend.PremsWishlist.add.modal.product",
* options={"seo"="false"}, methods={"POST"}
* )
*
* @param string $productId
* @param Request $request
* @param SalesChannelContext $context
*
* @return Response
*
* @throws InconsistentCriteriaIdsException
*/
public function modalAdd(string $productId, Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
$customer = $context->getCustomer();
$token = $context->getToken();
$quantity = (int) $request->get('quantity', 1);
$listIds = $request->get('lists');
$listName = $request->get('listName', '');
$visibility = $request->get('visibility', 0);
if ($visibility) {
$private = false;
} else {
$private = true;
}
// Check if User is Logged In
if ($this->getWishlistSettings()->isLoginRequired() && !$customer) {
$response = new JsonResponse([
'code' => 601,
'message' => 'User not logged in'
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
// ListIds given?
if (!empty($listIds)) {
$lists = $this->wishlistService->getWishlistsByIds($listIds, $context);
// Add Article to List
$this->wishlistService->addProductToWishlists($productId, $lists, $context->getContext(), $quantity);
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.article_added_successful'));
}
// Create new list
if (strlen($listName) > 0) {
$data = [
'name' => $listName,
'private' => $private,
'products' => [
[
'productId' => $productId,
'numberWished' => $quantity
]
],
'priority' => 0,
];
if ($customer) {
$data['customerId'] = $context->getCustomer()->getId();
} else {
$data['token'] = $token;
}
$data['salesChannelId'] = $context->getSalesChannelId();
$this->wishlistService->createWishlist([
$data
], $context->getContext());
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.article_added_successful'));
}
$response = new JsonResponse([
'success' => true
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* Function to change quantity of product in a wishlist
*
* @Route("/wishlist/changeProductQuantity/{wishlistId}/id/{id}", name="frontend.PremsWishlist.product.quantity.change", methods={"POST"})
*
* @param string $wishlistId
* @param string $id
* @param Request $request
* @param SalesChannelContext $context
* @return JsonResponse
* @throws InconsistentCriteriaIdsException
* @throws WishlistNotFoundException
*/
public function changeProductQuantity(string $wishlistId, string $id, Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
$customer = $context->getCustomer();
if ($this->getWishlistSettings()->isLoginRequired() && !$customer) {
throw new WishlistNotFoundException($wishlistId);
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context, true)) {
throw new WishlistNotFoundException($wishlistId);
}
$numberWished = (int) $request->request->get('numberWished', 0);
if ($numberWished) {
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context, true)) {
throw new WishlistNotFoundException($wishlistId);
}
$this->wishlistService->changeProductQuantity($id, $wishlist, $numberWished, $context->getContext());
/**$this->wishlistService->removeProductFromWishlist($wishlist, $id, $context->getContext());
$this->wishlistService->addProductToWishlists($productId, [$moveToWishlist], $context->getContext());*/
$response = new JsonResponse([
'success' => true
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
$response = new JsonResponse([
'success' => true
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* Adds all wishlist products to cart. Used for Wishlist Toolbox in Checkout
*
* @Route(
* "/wishlist/addToCart/{wishlistId}",
* name="frontend.PremsWishlist.add_to_cart",
* options={"seo"="false"},
* methods={"POST"}
* )
*
* @param string $wishlistId
* @param Request $request
* @param SalesChannelContext $context
*
* @return RedirectResponse
*
* @throws InconsistentCriteriaIdsException
* @throws WishlistNotFoundException
* @throws CartTokenNotFoundException
* @throws InvalidPayloadException
* @throws InvalidQuantityException
* @throws MixedLineItemTypeException
*/
public function addToCart(Request $request, SalesChannelContext $context, string $wishlistId = ""): RedirectResponse
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$wishlistSettings = $this->configService->getConfig();
if (strlen($wishlistId) == 0) {
$wishlistId = $request->request->get('wishlistId', '');
}
if (!$wishlistId) {
throw new \Exception("No wishlist id specified");
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context, true);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
if ($wishlist->isPrivate() && !$this->wishlistService->hasAccessToWishlist($wishlist, $context)) {
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
$this->wishlistService->addProductsToCart(
$request->request->getAlnum('token', $context->getToken()),
$wishlist,
$context
);
$this->addFlash('success', $this->trans('checkout.cartUpdateSuccess'));
$errors = $wishlist->getExtension('errors');
if ($errors){
$errors = $errors->getVars();
if (isset($errors['productNotFound'])) {
$this->addFlash('danger', $this->trans('checkout.product-not-found'));
}
}
if ($request->request->get('redirectToCart')) {
return $this->redirectToRoute('frontend.checkout.cart.page');
}
if ($wishlistSettings->getAddAllRedirectToCart()) {
if ($request->request->get('redirectToCheckout')) {
return $this->redirectToRoute('frontend.checkout.confirm.page');
} else {
return $this->redirectToRoute('frontend.checkout.cart.page');
}
} else {
if ($request->request->get('originDetailpage')) {
return $this->redirectToRoute('frontend.PremsWishlist.detail', ['wishlistId' => $wishlistId]);
} else {
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
}
}
/**
* Removes a wishlist
*
* @Route("/wishlist/remove/{wishlistId}", name="frontend.PremsWishlist.remove", methods={"GET"})
*
* @param string $wishlistId
* @param Request $request
* @param SalesChannelContext $context
* @return RedirectResponse
* @throws InconsistentCriteriaIdsException
* @throws WishlistNotFoundException
*/
public function removeWishlist(string $wishlistId, Request $request, SalesChannelContext $context): RedirectResponse
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
if ($this->wishlistService->hasAccessToWishlist($wishlist, $context, true)) {
$this->wishlistService->removeWishlist($wishlist, $context->getContext());
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.remove_successful'));
} else {
$this->addFlash('success', $this->trans('prems-wishlist.status_messages.remove_failed'));
}
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
/**
* Removes a product from wishlist
*
* @Route("/wishlist/remove/{id}/wishlist/{wishlistId}", name="frontend.PremsWishlist.product.remove", methods={"GET"})
*
* @param string $id
* @param string $wishlistId
* @param Request $request
* @param SalesChannelContext $context
* @return JsonResponse
* @throws InconsistentCriteriaIdsException
* @throws WishlistNotFoundException
*/
public function removeProductFromWishlist(string $id, string $wishlistId, Request $request, SalesChannelContext $context): RedirectResponse
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
if ($this->getWishlistSettings()->isLoginRequired()) {
$this->denyAccessUnlessLoggedIn();
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context, true)) {
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
$this->wishlistService->removeProductFromWishlist($wishlist, $id, $context->getContext());
return $this->redirectToRoute('frontend.PremsWishlist.index', ['wishlistId' => $wishlistId]);
}
/**
* Modal window to change product of a wishlist: Add comment, Number of products or move to another wishlist
*
* @Route("/wishlist/change/{wishlistId}/id/{id}", name="frontend.PremsWishlist.product.change", methods={"GET", "POST"})
*
* @param string $wishlistId
* @param string $id
* @param Request $request
* @param SalesChannelContext $context
* @return JsonResponse
* @throws InconsistentCriteriaIdsException
* @throws WishlistNotFoundException
*/
public function changeProductOfWishlist(string $wishlistId, string $id, Request $request, SalesChannelContext $context): Response
{
$page = $this->genericPageLoader->load($request, $context);
if ($page->getMetaInformation()) {
$page->getMetaInformation()->setRobots('noindex,nofollow');
}
$wishlist = $this->wishlistService->getWishlistById($wishlistId, $context);
if (!$wishlist) {
throw new WishlistNotFoundException($wishlistId);
}
if (!$this->wishlistService->hasAccessToWishlist($wishlist, $context, true)) {
throw new WishlistNotFoundException($wishlistId);
}
$moveToWishlistId = $request->request->get('moveToWishlist');
$hint = $request->request->get('hint');
$numberWished = (int) $request->request->get('numberWished', 0);
$numberGet = (int) $request->request->get('numberGet', 0);
$position = (int) $request->request->get('position', 0);
if ($moveToWishlistId) {
$moveToWishlist = $this->wishlistService->getWishlistById($moveToWishlistId, $context);
if (!$this->wishlistService->hasAccessToWishlist($moveToWishlist, $context, true)) {
throw new WishlistNotFoundException($moveToWishlistId);
}
$this->wishlistService->changeProductOfWishlist($id, $moveToWishlist, $hint, $numberWished, $numberGet, $position, $context->getContext());
/**$this->wishlistService->removeProductFromWishlist($wishlist, $id, $context->getContext());
$this->wishlistService->addProductToWishlists($productId, [$moveToWishlist], $context->getContext());*/
return $this->redirectToRoute('frontend.PremsWishlist.index');
}
$lists = $this->wishlistService->getWishlistsForUser($context);
foreach ($wishlist->getProducts() as $product) {
if ($product->getId() == $id) {
$hint = $product->getHint();
$numberWished = $product->getNumberWished();
$numberGet = $product->getNumberGet();
$position = $product->getPosition();
break;
}
}
$response = $this->renderStorefront('@PremsWishlist/storefront/page/wishlist/modal_change_product.html.twig', [
'premsWishlist' => $this->getWishlistSettings(),
'lists' => $lists,
'wishlistId' => $wishlistId,
'id' => $id,
'hint' => $hint,
'numberWished' => $numberWished,
'numberGet' => $numberGet,
'position' => $position
]);
$response->headers->set('X-Robots-Tag', 'noindex,nofollow');
return $response;
}
/**
* @throws CustomerNotLoggedInException
*/
protected function denyAccessUnlessLoggedIn(bool $allowGuest = false): void
{
/** @var RequestStack $requestStack */
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
if (!$request) {
throw new CustomerNotLoggedInException();
}
/** @var SalesChannelContext|null $context */
$context = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
if (
$context
&& $context->getCustomer()
&& (
$allowGuest === true
|| $context->getCustomer()->getGuest() === false
)
) {
return;
}
throw new CustomerNotLoggedInException();
}
}