Stock.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Controller\Add;
  7. use Magento\Framework\App\Action\HttpPostActionInterface;
  8. use Magento\ProductAlert\Controller\Add as AddController;
  9. use Magento\Framework\App\Action\Context;
  10. use Magento\Customer\Model\Session as CustomerSession;
  11. use Magento\Catalog\Api\ProductRepositoryInterface;
  12. use Magento\Framework\App\Action\Action;
  13. use Magento\Framework\Controller\ResultFactory;
  14. use Magento\Framework\Exception\NoSuchEntityException;
  15. use Magento\Store\Model\StoreManagerInterface;
  16. /**
  17. * Controller for notifying about stock.
  18. */
  19. class Stock extends AddController implements HttpPostActionInterface
  20. {
  21. /**
  22. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  23. */
  24. protected $productRepository;
  25. /**
  26. * @var StoreManagerInterface
  27. */
  28. private $storeManager;
  29. /**
  30. * @param \Magento\Framework\App\Action\Context $context
  31. * @param \Magento\Customer\Model\Session $customerSession
  32. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  33. * @param StoreManagerInterface|null $storeManager
  34. */
  35. public function __construct(
  36. Context $context,
  37. CustomerSession $customerSession,
  38. ProductRepositoryInterface $productRepository,
  39. StoreManagerInterface $storeManager = null
  40. ) {
  41. $this->productRepository = $productRepository;
  42. parent::__construct($context, $customerSession);
  43. $this->storeManager = $storeManager ?: $this->_objectManager
  44. ->get(\Magento\Store\Model\StoreManagerInterface::class);
  45. }
  46. /**
  47. * Method for adding info about product alert stock.
  48. *
  49. * @return \Magento\Framework\Controller\Result\Redirect
  50. */
  51. public function execute()
  52. {
  53. $backUrl = $this->getRequest()->getParam(Action::PARAM_NAME_URL_ENCODED);
  54. $productId = (int)$this->getRequest()->getParam('product_id');
  55. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  56. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  57. if (!$backUrl || !$productId) {
  58. $resultRedirect->setPath('/');
  59. return $resultRedirect;
  60. }
  61. try {
  62. /* @var $product \Magento\Catalog\Model\Product */
  63. $product = $this->productRepository->getById($productId);
  64. $store = $this->storeManager->getStore();
  65. /** @var \Magento\ProductAlert\Model\Stock $model */
  66. $model = $this->_objectManager->create(\Magento\ProductAlert\Model\Stock::class)
  67. ->setCustomerId($this->customerSession->getCustomerId())
  68. ->setProductId($product->getId())
  69. ->setWebsiteId($store->getWebsiteId())
  70. ->setStoreId($store->getId());
  71. $model->save();
  72. $this->messageManager->addSuccess(__('Alert subscription has been saved.'));
  73. } catch (NoSuchEntityException $noEntityException) {
  74. $this->messageManager->addError(__('There are not enough parameters.'));
  75. $resultRedirect->setUrl($backUrl);
  76. return $resultRedirect;
  77. } catch (\Exception $e) {
  78. $this->messageManager->addException(
  79. $e,
  80. __("The alert subscription couldn't update at this time. Please try again later.")
  81. );
  82. }
  83. $resultRedirect->setUrl($this->_redirect->getRedirectUrl());
  84. return $resultRedirect;
  85. }
  86. }