Price.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Store\Model\StoreManagerInterface;
  12. use Magento\Catalog\Api\ProductRepositoryInterface;
  13. use Magento\Framework\UrlInterface;
  14. use Magento\Framework\App\Action\Action;
  15. use Magento\Framework\Controller\ResultFactory;
  16. use Magento\Framework\Exception\NoSuchEntityException;
  17. /**
  18. * Controller for notifying about price.
  19. */
  20. class Price extends AddController implements HttpPostActionInterface
  21. {
  22. /**
  23. * @var \Magento\Store\Model\StoreManagerInterface
  24. */
  25. protected $storeManager;
  26. /**
  27. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  28. */
  29. protected $productRepository;
  30. /**
  31. * @param \Magento\Framework\App\Action\Context $context
  32. * @param \Magento\Customer\Model\Session $customerSession
  33. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  34. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  35. */
  36. public function __construct(
  37. Context $context,
  38. CustomerSession $customerSession,
  39. StoreManagerInterface $storeManager,
  40. ProductRepositoryInterface $productRepository
  41. ) {
  42. $this->storeManager = $storeManager;
  43. $this->productRepository = $productRepository;
  44. parent::__construct($context, $customerSession);
  45. }
  46. /**
  47. * Check if URL is internal
  48. *
  49. * @param string $url
  50. * @return bool
  51. */
  52. protected function isInternal($url)
  53. {
  54. if (strpos($url, 'http') === false) {
  55. return false;
  56. }
  57. $currentStore = $this->storeManager->getStore();
  58. return strpos($url, $currentStore->getBaseUrl()) === 0
  59. || strpos($url, $currentStore->getBaseUrl(UrlInterface::URL_TYPE_LINK, true)) === 0;
  60. }
  61. /**
  62. * Method for adding info about product alert price.
  63. *
  64. * @return \Magento\Framework\Controller\Result\Redirect
  65. */
  66. public function execute()
  67. {
  68. $backUrl = $this->getRequest()->getParam(Action::PARAM_NAME_URL_ENCODED);
  69. $productId = (int)$this->getRequest()->getParam('product_id');
  70. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  71. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  72. if (!$backUrl || !$productId) {
  73. $resultRedirect->setPath('/');
  74. return $resultRedirect;
  75. }
  76. $store = $this->storeManager->getStore();
  77. try {
  78. /* @var $product \Magento\Catalog\Model\Product */
  79. $product = $this->productRepository->getById($productId);
  80. /** @var \Magento\ProductAlert\Model\Price $model */
  81. $model = $this->_objectManager->create(\Magento\ProductAlert\Model\Price::class)
  82. ->setCustomerId($this->customerSession->getCustomerId())
  83. ->setProductId($product->getId())
  84. ->setPrice($product->getFinalPrice())
  85. ->setWebsiteId($store->getWebsiteId())
  86. ->setStoreId($store->getId());
  87. $model->save();
  88. $this->messageManager->addSuccess(__('You saved the alert subscription.'));
  89. } catch (NoSuchEntityException $noEntityException) {
  90. $this->messageManager->addError(__('There are not enough parameters.'));
  91. if ($this->isInternal($backUrl)) {
  92. $resultRedirect->setUrl($backUrl);
  93. } else {
  94. $resultRedirect->setPath('/');
  95. }
  96. return $resultRedirect;
  97. } catch (\Exception $e) {
  98. $this->messageManager->addException(
  99. $e,
  100. __("The alert subscription couldn't update at this time. Please try again later.")
  101. );
  102. }
  103. $resultRedirect->setUrl($this->_redirect->getRedirectUrl());
  104. return $resultRedirect;
  105. }
  106. }