123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?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;
- use Magento\Framework\Data\Form\FormKey\Validator;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Controller\ResultFactory;
- use Magento\Wishlist\Controller\WishlistProviderInterface;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class UpdateItemOptions extends \Magento\Wishlist\Controller\AbstractIndex
- {
- /**
- * @var WishlistProviderInterface
- */
- protected $wishlistProvider;
- /**
- * @var Session
- */
- protected $_customerSession;
- /**
- * @var ProductRepositoryInterface
- */
- protected $productRepository;
- /**
- * @var Validator
- */
- protected $formKeyValidator;
- /**
- * @param Action\Context $context
- * @param Session $customerSession
- * @param WishlistProviderInterface $wishlistProvider
- * @param ProductRepositoryInterface $productRepository
- * @param Validator $formKeyValidator
- */
- public function __construct(
- Action\Context $context,
- Session $customerSession,
- WishlistProviderInterface $wishlistProvider,
- ProductRepositoryInterface $productRepository,
- Validator $formKeyValidator
- ) {
- $this->_customerSession = $customerSession;
- $this->wishlistProvider = $wishlistProvider;
- $this->productRepository = $productRepository;
- $this->formKeyValidator = $formKeyValidator;
- parent::__construct($context);
- }
- /**
- * Action to accept new configuration for a wishlist item
- *
- * @return \Magento\Framework\Controller\Result\Redirect
- */
- 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('*/*/');
- }
- $productId = (int)$this->getRequest()->getParam('product');
- if (!$productId) {
- $resultRedirect->setPath('*/');
- return $resultRedirect;
- }
- try {
- $product = $this->productRepository->getById($productId);
- } catch (NoSuchEntityException $e) {
- $product = null;
- }
- if (!$product || !$product->isVisibleInCatalog()) {
- $this->messageManager->addError(__('We can\'t specify a product.'));
- $resultRedirect->setPath('*/');
- return $resultRedirect;
- }
- try {
- $id = (int)$this->getRequest()->getParam('id');
- /* @var \Magento\Wishlist\Model\Item */
- $item = $this->_objectManager->create(\Magento\Wishlist\Model\Item::class);
- $item->load($id);
- $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
- if (!$wishlist) {
- $resultRedirect->setPath('*/');
- return $resultRedirect;
- }
- $buyRequest = new \Magento\Framework\DataObject($this->getRequest()->getParams());
- $wishlist->updateItem($id, $buyRequest)->save();
- $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
- $this->_eventManager->dispatch(
- 'wishlist_update_item',
- ['wishlist' => $wishlist, 'product' => $product, 'item' => $wishlist->getItem($id)]
- );
- $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
- $message = __('%1 has been updated in your Wish List.', $product->getName());
- $this->messageManager->addSuccess($message);
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $this->messageManager->addError($e->getMessage());
- } catch (\Exception $e) {
- $this->messageManager->addError(__('We can\'t update your Wish List right now.'));
- $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
- }
- $resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
- return $resultRedirect;
- }
- }
|