Price.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Price 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. /* @var $product \Magento\Catalog\Model\Product */
  46. $product = $this->productRepository->getById($productId);
  47. if (!$product->isVisibleInCatalog()) {
  48. throw new NoSuchEntityException();
  49. }
  50. /** @var \Magento\ProductAlert\Model\Price $model */
  51. $model = $this->_objectManager->create(\Magento\ProductAlert\Model\Price::class)
  52. ->setCustomerId($this->customerSession->getCustomerId())
  53. ->setProductId($product->getId())
  54. ->setWebsiteId(
  55. $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
  56. ->getStore()
  57. ->getWebsiteId()
  58. )
  59. ->loadByParam();
  60. if ($model->getId()) {
  61. $model->delete();
  62. }
  63. $this->messageManager->addSuccess(__('You deleted the alert subscription.'));
  64. } catch (NoSuchEntityException $noEntityException) {
  65. $this->messageManager->addError(__("The product wasn't found. Verify the product and try again."));
  66. $resultRedirect->setPath('customer/account/');
  67. return $resultRedirect;
  68. } catch (\Exception $e) {
  69. $this->messageManager->addException(
  70. $e,
  71. __("The alert subscription couldn't update at this time. Please try again later.")
  72. );
  73. }
  74. $resultRedirect->setUrl($product->getProductUrl());
  75. return $resultRedirect;
  76. }
  77. }