Skip to content

Commit e03bd31

Browse files
authored
ENGCOM-5371: #22814: Product stock alert - unsubscribe not working #23459
2 parents 1878c70 + 1689af8 commit e03bd31

File tree

1 file changed

+64
-18
lines changed
  • app/code/Magento/ProductAlert/Controller/Unsubscribe

1 file changed

+64
-18
lines changed

app/code/Magento/ProductAlert/Controller/Unsubscribe/Stock.php

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,91 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\ProductAlert\Controller\Unsubscribe;
810

9-
use Magento\ProductAlert\Controller\Unsubscribe as UnsubscribeController;
10-
use Magento\Framework\App\Action\Context;
11-
use Magento\Customer\Model\Session as CustomerSession;
1211
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Customer\Model\Session as CustomerSession;
13+
use Magento\Framework\App\Action\Context;
14+
use Magento\Framework\App\Action\HttpPostActionInterface;
15+
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Controller\Result\Redirect;
1317
use Magento\Framework\Controller\ResultFactory;
1418
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\ProductAlert\Controller\Unsubscribe as UnsubscribeController;
20+
use Magento\ProductAlert\Model\StockFactory;
21+
use Magento\Store\Model\StoreManagerInterface;
22+
use Magento\Catalog\Api\Data\ProductInterface;
1523

16-
class Stock extends UnsubscribeController
24+
/**
25+
* Unsubscribing from 'back in stock alert'.
26+
*/
27+
class Stock extends UnsubscribeController implements HttpPostActionInterface
1728
{
1829
/**
19-
* @var \Magento\Catalog\Api\ProductRepositoryInterface
30+
* @var ProductRepositoryInterface
2031
*/
2132
protected $productRepository;
2233

2334
/**
24-
* @param \Magento\Framework\App\Action\Context $context
25-
* @param \Magento\Customer\Model\Session $customerSession
26-
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
35+
* @var StoreManagerInterface|null
36+
*/
37+
private $storeManager;
38+
39+
/**
40+
* @var StockFactory|null
41+
*/
42+
private $stockFactory;
43+
44+
/**
45+
* @param Context $context
46+
* @param CustomerSession $customerSession
47+
* @param ProductRepositoryInterface $productRepository
48+
* @param StoreManagerInterface|null $storeManager
49+
* @param StockFactory|null $stockFactory
2750
*/
2851
public function __construct(
2952
Context $context,
3053
CustomerSession $customerSession,
31-
ProductRepositoryInterface $productRepository
54+
ProductRepositoryInterface $productRepository,
55+
StoreManagerInterface $storeManager = null,
56+
StockFactory $stockFactory = null
3257
) {
3358
$this->productRepository = $productRepository;
59+
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
60+
$this->stockFactory = $stockFactory ?? ObjectManager::getInstance()->get(StockFactory::class);
3461
parent::__construct($context, $customerSession);
3562
}
3663

3764
/**
38-
* @return \Magento\Framework\Controller\Result\Redirect
65+
* Unsubscribing from 'back in stock alert'.
66+
*
67+
* @return Redirect
3968
*/
4069
public function execute()
4170
{
4271
$productId = (int)$this->getRequest()->getParam('product');
43-
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
72+
/** @var Redirect $resultRedirect */
4473
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
4574
if (!$productId) {
4675
$resultRedirect->setPath('/');
4776
return $resultRedirect;
4877
}
4978

5079
try {
51-
$product = $this->productRepository->getById($productId);
52-
if (!$product->isVisibleInCatalog()) {
53-
throw new NoSuchEntityException();
54-
}
55-
56-
$model = $this->_objectManager->create(\Magento\ProductAlert\Model\Stock::class)
80+
$product = $this->retrieveProduct($productId);
81+
$model = $this->stockFactory->create()
5782
->setCustomerId($this->customerSession->getCustomerId())
5883
->setProductId($product->getId())
5984
->setWebsiteId(
60-
$this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
85+
$this->storeManager
6186
->getStore()
6287
->getWebsiteId()
88+
)->setStoreId(
89+
$this->storeManager
90+
->getStore()
91+
->getId()
6392
)
6493
->loadByParam();
6594
if ($model->getId()) {
@@ -79,4 +108,21 @@ public function execute()
79108
$resultRedirect->setUrl($product->getProductUrl());
80109
return $resultRedirect;
81110
}
111+
112+
/**
113+
* Retrieving product
114+
*
115+
* @param int $productId
116+
*
117+
* @return ProductInterface
118+
* @throws NoSuchEntityException
119+
*/
120+
private function retrieveProduct(int $productId): ProductInterface
121+
{
122+
$product = $this->productRepository->getById($productId);
123+
if (!$product->isVisibleInCatalog()) {
124+
throw new NoSuchEntityException();
125+
}
126+
return $product;
127+
}
82128
}

0 commit comments

Comments
 (0)