123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Wishlist\Controller\Index;
- use Magento\Framework\App\Action;
- use Magento\Framework\Data\Form\FormKey\Validator;
- use Magento\Framework\Exception\NotFoundException;
- use Magento\Framework\Controller\ResultFactory;
- use Magento\Wishlist\Controller\WishlistProviderInterface;
- use Magento\Wishlist\Model\Item;
- use Magento\Wishlist\Model\Product\AttributeValueProvider;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Remove extends \Magento\Wishlist\Controller\AbstractIndex
- {
- /**
- * @var WishlistProviderInterface
- */
- protected $wishlistProvider;
- /**
- * @var Validator
- */
- protected $formKeyValidator;
- /**
- * @var AttributeValueProvider
- */
- private $attributeValueProvider;
- /**
- * @param Action\Context $context
- * @param WishlistProviderInterface $wishlistProvider
- * @param Validator $formKeyValidator
- * @param AttributeValueProvider|null $attributeValueProvider
- */
- public function __construct(
- Action\Context $context,
- WishlistProviderInterface $wishlistProvider,
- Validator $formKeyValidator,
- AttributeValueProvider $attributeValueProvider = null
- ) {
- $this->wishlistProvider = $wishlistProvider;
- $this->formKeyValidator = $formKeyValidator;
- $this->attributeValueProvider = $attributeValueProvider
- ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
- parent::__construct($context);
- }
- /**
- * Remove item
- *
- * @return \Magento\Framework\Controller\Result\Redirect
- * @throws NotFoundException
- */
- 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('*/*/');
- }
- $id = (int)$this->getRequest()->getParam('item');
- /** @var Item $item */
- $item = $this->_objectManager->create(Item::class)->load($id);
- if (!$item->getId()) {
- throw new NotFoundException(__('Page not found.'));
- }
- $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
- if (!$wishlist) {
- throw new NotFoundException(__('Page not found.'));
- }
- try {
- $item->delete();
- $wishlist->save();
- $productName = $this->attributeValueProvider
- ->getRawAttributeValue($item->getProductId(), 'name');
- $this->messageManager->addComplexSuccessMessage(
- 'removeWishlistItemSuccessMessage',
- [
- 'product_name' => $productName,
- ]
- );
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $this->messageManager->addError(
- __('We can\'t delete the item from Wish List right now because of an error: %1.', $e->getMessage())
- );
- } catch (\Exception $e) {
- $this->messageManager->addError(__('We can\'t delete the item from the Wish List right now.'));
- }
- $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
- $refererUrl = $this->_redirect->getRefererUrl();
- if ($refererUrl) {
- $redirectUrl = $refererUrl;
- } else {
- $redirectUrl = $this->_redirect->getRedirectUrl($this->_url->getUrl('*/*'));
- }
- $resultRedirect->setUrl($redirectUrl);
- return $resultRedirect;
- }
- }
|