Remove.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Wishlist\Controller\Index;
  7. use Magento\Framework\App\Action;
  8. use Magento\Framework\Data\Form\FormKey\Validator;
  9. use Magento\Framework\Exception\NotFoundException;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\Wishlist\Controller\WishlistProviderInterface;
  12. use Magento\Wishlist\Model\Item;
  13. use Magento\Wishlist\Model\Product\AttributeValueProvider;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Remove extends \Magento\Wishlist\Controller\AbstractIndex
  18. {
  19. /**
  20. * @var WishlistProviderInterface
  21. */
  22. protected $wishlistProvider;
  23. /**
  24. * @var Validator
  25. */
  26. protected $formKeyValidator;
  27. /**
  28. * @var AttributeValueProvider
  29. */
  30. private $attributeValueProvider;
  31. /**
  32. * @param Action\Context $context
  33. * @param WishlistProviderInterface $wishlistProvider
  34. * @param Validator $formKeyValidator
  35. * @param AttributeValueProvider|null $attributeValueProvider
  36. */
  37. public function __construct(
  38. Action\Context $context,
  39. WishlistProviderInterface $wishlistProvider,
  40. Validator $formKeyValidator,
  41. AttributeValueProvider $attributeValueProvider = null
  42. ) {
  43. $this->wishlistProvider = $wishlistProvider;
  44. $this->formKeyValidator = $formKeyValidator;
  45. $this->attributeValueProvider = $attributeValueProvider
  46. ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
  47. parent::__construct($context);
  48. }
  49. /**
  50. * Remove item
  51. *
  52. * @return \Magento\Framework\Controller\Result\Redirect
  53. * @throws NotFoundException
  54. */
  55. public function execute()
  56. {
  57. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  58. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  59. if (!$this->formKeyValidator->validate($this->getRequest())) {
  60. return $resultRedirect->setPath('*/*/');
  61. }
  62. $id = (int)$this->getRequest()->getParam('item');
  63. /** @var Item $item */
  64. $item = $this->_objectManager->create(Item::class)->load($id);
  65. if (!$item->getId()) {
  66. throw new NotFoundException(__('Page not found.'));
  67. }
  68. $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
  69. if (!$wishlist) {
  70. throw new NotFoundException(__('Page not found.'));
  71. }
  72. try {
  73. $item->delete();
  74. $wishlist->save();
  75. $productName = $this->attributeValueProvider
  76. ->getRawAttributeValue($item->getProductId(), 'name');
  77. $this->messageManager->addComplexSuccessMessage(
  78. 'removeWishlistItemSuccessMessage',
  79. [
  80. 'product_name' => $productName,
  81. ]
  82. );
  83. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  84. $this->messageManager->addError(
  85. __('We can\'t delete the item from Wish List right now because of an error: %1.', $e->getMessage())
  86. );
  87. } catch (\Exception $e) {
  88. $this->messageManager->addError(__('We can\'t delete the item from the Wish List right now.'));
  89. }
  90. $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
  91. $refererUrl = $this->_redirect->getRefererUrl();
  92. if ($refererUrl) {
  93. $redirectUrl = $refererUrl;
  94. } else {
  95. $redirectUrl = $this->_redirect->getRedirectUrl($this->_url->getUrl('*/*'));
  96. }
  97. $resultRedirect->setUrl($redirectUrl);
  98. return $resultRedirect;
  99. }
  100. }