Stock.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Controller\Unsubscribe;
  7. use Magento\ProductAlert\Controller\Unsubscribe as UnsubscribeController;
  8. use Magento\Framework\App\Action\Context;
  9. use Magento\Customer\Model\Session as CustomerSession;
  10. use Magento\Catalog\Api\ProductRepositoryInterface;
  11. use Magento\Framework\Controller\ResultFactory;
  12. use Magento\Framework\Exception\NoSuchEntityException;
  13. class Stock extends UnsubscribeController
  14. {
  15. /**
  16. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  17. */
  18. protected $productRepository;
  19. /**
  20. * @param \Magento\Framework\App\Action\Context $context
  21. * @param \Magento\Customer\Model\Session $customerSession
  22. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  23. */
  24. public function __construct(
  25. Context $context,
  26. CustomerSession $customerSession,
  27. ProductRepositoryInterface $productRepository
  28. ) {
  29. $this->productRepository = $productRepository;
  30. parent::__construct($context, $customerSession);
  31. }
  32. /**
  33. * @return \Magento\Framework\Controller\Result\Redirect
  34. */
  35. public function execute()
  36. {
  37. $productId = (int)$this->getRequest()->getParam('product');
  38. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  39. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  40. if (!$productId) {
  41. $resultRedirect->setPath('/');
  42. return $resultRedirect;
  43. }
  44. try {
  45. $product = $this->productRepository->getById($productId);
  46. if (!$product->isVisibleInCatalog()) {
  47. throw new NoSuchEntityException();
  48. }
  49. $model = $this->_objectManager->create(\Magento\ProductAlert\Model\Stock::class)
  50. ->setCustomerId($this->customerSession->getCustomerId())
  51. ->setProductId($product->getId())
  52. ->setWebsiteId(
  53. $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
  54. ->getStore()
  55. ->getWebsiteId()
  56. )
  57. ->loadByParam();
  58. if ($model->getId()) {
  59. $model->delete();
  60. }
  61. $this->messageManager->addSuccess(__('You will no longer receive stock alerts for this product.'));
  62. } catch (NoSuchEntityException $noEntityException) {
  63. $this->messageManager->addError(__('The product was not found.'));
  64. $resultRedirect->setPath('customer/account/');
  65. return $resultRedirect;
  66. } catch (\Exception $e) {
  67. $this->messageManager->addException(
  68. $e,
  69. __("The alert subscription couldn't update at this time. Please try again later.")
  70. );
  71. }
  72. $resultRedirect->setUrl($product->getProductUrl());
  73. return $resultRedirect;
  74. }
  75. }