Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Remove.php
Go to the documentation of this file.
1 <?php
7 
15 
20 {
24  protected $wishlistProvider;
25 
29  protected $formKeyValidator;
30 
34  private $attributeValueProvider;
35 
42  public function __construct(
43  Action\Context $context,
46  AttributeValueProvider $attributeValueProvider = null
47  ) {
48  $this->wishlistProvider = $wishlistProvider;
49  $this->formKeyValidator = $formKeyValidator;
50  $this->attributeValueProvider = $attributeValueProvider
51  ?: \Magento\Framework\App\ObjectManager::getInstance()->get(AttributeValueProvider::class);
52  parent::__construct($context);
53  }
54 
61  public function execute()
62  {
64  $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
65  if (!$this->formKeyValidator->validate($this->getRequest())) {
66  return $resultRedirect->setPath('*/*/');
67  }
68 
69  $id = (int)$this->getRequest()->getParam('item');
71  $item = $this->_objectManager->create(Item::class)->load($id);
72  if (!$item->getId()) {
73  throw new NotFoundException(__('Page not found.'));
74  }
75  $wishlist = $this->wishlistProvider->getWishlist($item->getWishlistId());
76  if (!$wishlist) {
77  throw new NotFoundException(__('Page not found.'));
78  }
79  try {
80  $item->delete();
81  $wishlist->save();
82  $productName = $this->attributeValueProvider
83  ->getRawAttributeValue($item->getProductId(), 'name');
84  $this->messageManager->addComplexSuccessMessage(
85  'removeWishlistItemSuccessMessage',
86  [
87  'product_name' => $productName,
88  ]
89  );
90  } catch (\Magento\Framework\Exception\LocalizedException $e) {
91  $this->messageManager->addError(
92  __('We can\'t delete the item from Wish List right now because of an error: %1.', $e->getMessage())
93  );
94  } catch (\Exception $e) {
95  $this->messageManager->addError(__('We can\'t delete the item from the Wish List right now.'));
96  }
97 
98  $this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();
99  $refererUrl = $this->_redirect->getRefererUrl();
100  if ($refererUrl) {
101  $redirectUrl = $refererUrl;
102  } else {
103  $redirectUrl = $this->_redirect->getRedirectUrl($this->_url->getUrl('*/*'));
104  }
105  $resultRedirect->setUrl($redirectUrl);
106  return $resultRedirect;
107  }
108 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$wishlist
Definition: wishlist.php:10
__construct(Action\Context $context, WishlistProviderInterface $wishlistProvider, Validator $formKeyValidator, AttributeValueProvider $attributeValueProvider=null)
Definition: Remove.php:42