123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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\Framework\App\Action;
- use Magento\Framework\Data\Form\FormKey\Validator;
- use Magento\Framework\Exception\NotFoundException;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Controller\ResultFactory;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Add extends \Magento\Wishlist\Controller\AbstractIndex
- {
- /**
- * @var \Magento\Wishlist\Controller\WishlistProviderInterface
- */
- protected $wishlistProvider;
- /**
- * @var \Magento\Customer\Model\Session
- */
- protected $_customerSession;
- /**
- * @var ProductRepositoryInterface
- */
- protected $productRepository;
- /**
- * @var Validator
- */
- protected $formKeyValidator;
- /**
- * @param Action\Context $context
- * @param \Magento\Customer\Model\Session $customerSession
- * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
- * @param ProductRepositoryInterface $productRepository
- * @param Validator $formKeyValidator
- */
- public function __construct(
- Action\Context $context,
- \Magento\Customer\Model\Session $customerSession,
- \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
- ProductRepositoryInterface $productRepository,
- Validator $formKeyValidator
- ) {
- $this->_customerSession = $customerSession;
- $this->wishlistProvider = $wishlistProvider;
- $this->productRepository = $productRepository;
- $this->formKeyValidator = $formKeyValidator;
- parent::__construct($context);
- }
- /**
- * Adding new item
- *
- * @return \Magento\Framework\Controller\Result\Redirect
- * @throws NotFoundException
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- public function execute()
- {
- /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
- $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
- if (!$this->formKeyValidator->validate($this->getRequest())) {
- return $resultRedirect->setPath('*/');
- }
- $wishlist = $this->wishlistProvider->getWishlist();
- if (!$wishlist) {
- throw new NotFoundException(__('Page not found.'));
- }
- $session = $this->_customerSession;
- $requestParams = $this->getRequest()->getParams();
- if ($session->getBeforeWishlistRequest()) {
- $requestParams = $session->getBeforeWishlistRequest();
- $session->unsBeforeWishlistRequest();
- }
- $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 \Magento\Framework\Exception\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 {
- $referer = $this->_redirect->getRefererUrl();
- }
- $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
- $this->messageManager->addComplexSuccessMessage(
- 'addProductSuccessMessage',
- [
- 'product_name' => $product->getName(),
- 'referer' => $referer
- ]
- );
- } catch (\Magento\Framework\Exception\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.')
- );
- }
- $resultRedirect->setPath('*', ['wishlist_id' => $wishlist->getId()]);
- return $resultRedirect;
- }
- }
|