![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/cartforge.co/vendor/magento/module-wishlist/Controller/Index/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Wishlist\Controller\Index; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Customer\Model\Session; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Action\HttpGetActionInterface; use Magento\Framework\App\Action\HttpPostActionInterface; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Data\Form\FormKey\Validator; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NotFoundException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Controller\ResultFactory; use Magento\Framework\App\ObjectManager; use Magento\Framework\UrlInterface; use Magento\Framework\App\Response\RedirectInterface; use Magento\Framework\Controller\ResultInterface; use Magento\Wishlist\Controller\WishlistProviderInterface; /** * Wish list Add controller * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Add extends \Magento\Wishlist\Controller\AbstractIndex implements HttpPostActionInterface, HttpGetActionInterface { /** * @var WishlistProviderInterface */ protected $wishlistProvider; /** * @var Session */ protected $_customerSession; /** * @var ProductRepositoryInterface */ protected $productRepository; /** * @var Validator */ protected $formKeyValidator; /** * @var RedirectInterface */ private $redirect; /** * @var UrlInterface */ private $urlBuilder; /** * @param Context $context * @param Session $customerSession * @param WishlistProviderInterface $wishlistProvider * @param ProductRepositoryInterface $productRepository * @param Validator $formKeyValidator * @param RedirectInterface|null $redirect * @param UrlInterface|null $urlBuilder */ public function __construct( Context $context, Session $customerSession, WishlistProviderInterface $wishlistProvider, ProductRepositoryInterface $productRepository, Validator $formKeyValidator, ?RedirectInterface $redirect = null, ?UrlInterface $urlBuilder = null ) { $this->_customerSession = $customerSession; $this->wishlistProvider = $wishlistProvider; $this->productRepository = $productRepository; $this->formKeyValidator = $formKeyValidator; $this->redirect = $redirect ?: ObjectManager::getInstance()->get(RedirectInterface::class); $this->urlBuilder = $urlBuilder ?: ObjectManager::getInstance()->get(UrlInterface::class); parent::__construct($context); } /** * Adding new item * * @return ResultInterface * @throws NotFoundException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.UnusedLocalVariable) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function execute() { /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $session = $this->_customerSession; $requestParams = $this->getRequest()->getParams(); if (!$this->formKeyValidator->validate($this->getRequest())) { return $resultRedirect->setPath('*/'); } $wishlist = $this->wishlistProvider->getWishlist(); if (!$wishlist) { throw new NotFoundException(__('Page not found.')); } $productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null; if (!$productId) { $resultRedirect->setPath('*/'); return $resultRedirect; } try { $product = $this->productRepository->getById($productId); } catch (NoSuchEntityException $e) { $product = null; } if (!$product || !$product->isVisibleInCatalog()) { $this->messageManager->addErrorMessage(__('We can\'t specify a product.')); $resultRedirect->setPath('*/'); return $resultRedirect; } try { $buyRequest = new \Magento\Framework\DataObject($requestParams); $result = $wishlist->addNewItem($product, $buyRequest); if (is_string($result)) { throw new LocalizedException(__($result)); } if ($wishlist->isObjectNew()) { $wishlist->save(); } $this->_eventManager->dispatch( 'wishlist_add_product', ['wishlist' => $wishlist, 'product' => $product, 'item' => $result] ); $referer = $session->getBeforeWishlistUrl(); if ($referer) { $session->setBeforeWishlistUrl(null); } else { // phpcs:ignore $referer = $this->_redirect->getRefererUrl(); } $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate(); $this->messageManager->addComplexSuccessMessage( 'addProductSuccessMessage', [ 'product_name' => $product->getName(), 'referer' => $referer ] ); // phpcs:disable Magento2.Exceptions.ThrowCatch } catch (LocalizedException $e) { $this->messageManager->addErrorMessage( __('We can\'t add the item to Wish List right now: %1.', $e->getMessage()) ); } catch (\Exception $e) { $this->messageManager->addExceptionMessage( $e, __('We can\'t add the item to Wish List right now.') ); } if ($this->getRequest()->isAjax()) { $url = $this->urlBuilder->getUrl( '*', $this->redirect->updatePathParams( ['wishlist_id' => $wishlist->getId()] ) ); /** @var Json $resultJson */ $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON); $resultJson->setData(['backUrl' => $url]); return $resultJson; } $resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]); return $resultRedirect; } }