Skip to content

Commit ad0bd87

Browse files
committed
Merge remote-tracking branch 'github-magento2ce/MAGETWO-91650' into EPAM-PR-18
2 parents 9fdc8c1 + e767121 commit ad0bd87

File tree

8 files changed

+130
-36
lines changed

8 files changed

+130
-36
lines changed

app/code/Magento/ProductAlert/Controller/Add/Price.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\ProductAlert\Controller\Add;
88

9+
use Magento\Framework\App\Action\HttpPostActionInterface;
910
use Magento\ProductAlert\Controller\Add as AddController;
1011
use Magento\Framework\App\Action\Context;
1112
use Magento\Customer\Model\Session as CustomerSession;
@@ -16,7 +17,10 @@
1617
use Magento\Framework\Controller\ResultFactory;
1718
use Magento\Framework\Exception\NoSuchEntityException;
1819

19-
class Price extends AddController
20+
/**
21+
* Controller for notifying about price.
22+
*/
23+
class Price extends AddController implements HttpPostActionInterface
2024
{
2125
/**
2226
* @var \Magento\Store\Model\StoreManagerInterface
@@ -62,6 +66,8 @@ protected function isInternal($url)
6266
}
6367

6468
/**
69+
* Method for adding info about product alert price.
70+
*
6571
* @return \Magento\Framework\Controller\Result\Redirect
6672
*/
6773
public function execute()
@@ -75,6 +81,7 @@ public function execute()
7581
return $resultRedirect;
7682
}
7783

84+
$store = $this->storeManager->getStore();
7885
try {
7986
/* @var $product \Magento\Catalog\Model\Product */
8087
$product = $this->productRepository->getById($productId);
@@ -83,11 +90,8 @@ public function execute()
8390
->setCustomerId($this->customerSession->getCustomerId())
8491
->setProductId($product->getId())
8592
->setPrice($product->getFinalPrice())
86-
->setWebsiteId(
87-
$this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
88-
->getStore()
89-
->getWebsiteId()
90-
);
93+
->setWebsiteId($store->getWebsiteId())
94+
->setStoreId($store->getId());
9195
$model->save();
9296
$this->messageManager->addSuccess(__('You saved the alert subscription.'));
9397
} catch (NoSuchEntityException $noEntityException) {

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,52 @@
66

77
namespace Magento\ProductAlert\Controller\Add;
88

9+
use Magento\Framework\App\Action\HttpPostActionInterface;
910
use Magento\ProductAlert\Controller\Add as AddController;
1011
use Magento\Framework\App\Action\Context;
1112
use Magento\Customer\Model\Session as CustomerSession;
1213
use Magento\Catalog\Api\ProductRepositoryInterface;
1314
use Magento\Framework\App\Action\Action;
1415
use Magento\Framework\Controller\ResultFactory;
1516
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Store\Model\StoreManagerInterface;
1618

17-
class Stock extends AddController
19+
/**
20+
* Controller for notifying about stock.
21+
*/
22+
class Stock extends AddController implements HttpPostActionInterface
1823
{
1924
/**
2025
* @var \Magento\Catalog\Api\ProductRepositoryInterface
2126
*/
2227
protected $productRepository;
2328

29+
/**
30+
* @var StoreManagerInterface
31+
*/
32+
private $storeManager;
33+
2434
/**
2535
* @param \Magento\Framework\App\Action\Context $context
2636
* @param \Magento\Customer\Model\Session $customerSession
2737
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
38+
* @param StoreManagerInterface|null $storeManager
2839
*/
2940
public function __construct(
3041
Context $context,
3142
CustomerSession $customerSession,
32-
ProductRepositoryInterface $productRepository
43+
ProductRepositoryInterface $productRepository,
44+
StoreManagerInterface $storeManager = null
3345
) {
3446
$this->productRepository = $productRepository;
3547
parent::__construct($context, $customerSession);
48+
$this->storeManager = $storeManager ?: $this->_objectManager
49+
->get(\Magento\Store\Model\StoreManagerInterface::class);
3650
}
3751

3852
/**
53+
* Method for adding info about product alert stock.
54+
*
3955
* @return \Magento\Framework\Controller\Result\Redirect
4056
*/
4157
public function execute()
@@ -52,15 +68,13 @@ public function execute()
5268
try {
5369
/* @var $product \Magento\Catalog\Model\Product */
5470
$product = $this->productRepository->getById($productId);
71+
$store = $this->storeManager->getStore();
5572
/** @var \Magento\ProductAlert\Model\Stock $model */
5673
$model = $this->_objectManager->create(\Magento\ProductAlert\Model\Stock::class)
5774
->setCustomerId($this->customerSession->getCustomerId())
5875
->setProductId($product->getId())
59-
->setWebsiteId(
60-
$this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)
61-
->getStore()
62-
->getWebsiteId()
63-
);
76+
->setWebsiteId($store->getWebsiteId())
77+
->setStoreId($store->getId());
6478
$model->save();
6579
$this->messageManager->addSuccess(__('Alert subscription has been saved.'));
6680
} catch (NoSuchEntityException $noEntityException) {

app/code/Magento/ProductAlert/Model/Email.php

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ class Email extends AbstractModel
136136
*/
137137
protected $_customerHelper;
138138

139+
/**
140+
* @var int
141+
*/
142+
private $storeId = null;
143+
139144
/**
140145
* @param Context $context
141146
* @param Registry $registry
@@ -210,6 +215,18 @@ public function setWebsite(\Magento\Store\Model\Website $website)
210215
return $this;
211216
}
212217

218+
/**
219+
* Set store id from product alert.
220+
*
221+
* @param int $storeId
222+
* @return $this
223+
*/
224+
public function setStoreId(int $storeId)
225+
{
226+
$this->storeId = $storeId;
227+
return $this;
228+
}
229+
213230
/**
214231
* Set website id
215232
*
@@ -330,30 +347,18 @@ protected function _getStockBlock()
330347
*/
331348
public function send()
332349
{
333-
if ($this->_website === null || $this->_customer === null) {
334-
return false;
335-
}
336-
337-
if (!$this->_website->getDefaultGroup() || !$this->_website->getDefaultGroup()->getDefaultStore()) {
338-
return false;
339-
}
340-
341-
if (!in_array($this->_type, ['price', 'stock'])) {
350+
if ($this->_website === null || $this->_customer === null || !$this->isExistDefaultStore()) {
342351
return false;
343352
}
344353

345354
$products = $this->getProducts();
346-
if (count($products) === 0) {
347-
return false;
348-
}
349-
350355
$templateConfigPath = $this->getTemplateConfigPath();
351-
if (!$templateConfigPath) {
356+
if (!in_array($this->_type, ['price', 'stock']) || count($products) === 0 || !$templateConfigPath) {
352357
return false;
353358
}
354359

355-
$store = $this->getStore((int) $this->_customer->getStoreId());
356-
$storeId = $store->getId();
360+
$storeId = $this->storeId ?: (int) $this->_customer->getStoreId();
361+
$store = $this->getStore($storeId);
357362

358363
$this->_appEmulation->startEnvironmentEmulation($storeId);
359364

@@ -405,16 +410,13 @@ public function send()
405410
/**
406411
* Retrieve the store for the email
407412
*
408-
* @param int|null $customerStoreId
409-
*
413+
* @param int $storeId
410414
* @return StoreInterface
411415
* @throws NoSuchEntityException
412416
*/
413-
private function getStore(?int $customerStoreId): StoreInterface
417+
private function getStore(int $storeId): StoreInterface
414418
{
415-
return $customerStoreId > 0
416-
? $this->_storeManager->getStore($customerStoreId)
417-
: $this->_website->getDefaultStore();
419+
return $this->_storeManager->getStore($storeId);
418420
}
419421

420422
/**
@@ -453,4 +455,17 @@ private function getTemplateConfigPath(): string
453455
? self::XML_PATH_EMAIL_PRICE_TEMPLATE
454456
: self::XML_PATH_EMAIL_STOCK_TEMPLATE;
455457
}
458+
459+
/**
460+
* Check if exists default store.
461+
*
462+
* @return bool
463+
*/
464+
private function isExistDefaultStore(): bool
465+
{
466+
if (!$this->_website->getDefaultGroup() || !$this->_website->getDefaultGroup()->getDefaultStore()) {
467+
return false;
468+
}
469+
return true;
470+
}
456471
}

app/code/Magento/ProductAlert/Model/Observer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ protected function _processPrice(\Magento\ProductAlert\Model\Email $email)
218218
$previousCustomer = null;
219219
$email->setWebsite($website);
220220
foreach ($collection as $alert) {
221+
$this->setAlertStoreId($alert, $email);
221222
try {
222223
if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
223224
$customer = $this->customerRepository->getById($alert->getCustomerId());
@@ -311,6 +312,7 @@ protected function _processStock(\Magento\ProductAlert\Model\Email $email)
311312
$previousCustomer = null;
312313
$email->setWebsite($website);
313314
foreach ($collection as $alert) {
315+
$this->setAlertStoreId($alert, $email);
314316
try {
315317
if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
316318
$customer = $this->customerRepository->getById($alert->getCustomerId());
@@ -427,4 +429,21 @@ public function process()
427429

428430
return $this;
429431
}
432+
433+
/**
434+
* Set alert store id.
435+
*
436+
* @param \Magento\ProductAlert\Model\Price|\Magento\ProductAlert\Model\Stock $alert
437+
* @param Email $email
438+
* @return Observer
439+
*/
440+
private function setAlertStoreId($alert, \Magento\ProductAlert\Model\Email $email) : Observer
441+
{
442+
$alertStoreId = $alert->getStoreId();
443+
if ($alertStoreId) {
444+
$email->setStoreId((int)$alertStoreId);
445+
}
446+
447+
return $this;
448+
}
430449
}

app/code/Magento/ProductAlert/Model/Price.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
* @method \Magento\ProductAlert\Model\Price setSendCount(int $value)
2727
* @method int getStatus()
2828
* @method \Magento\ProductAlert\Model\Price setStatus(int $value)
29+
* @method int getStoreId()
30+
* @method \Magento\ProductAlert\Model\Stock setStoreId(int $value)
2931
*
3032
* @author Magento Core Team <[email protected]>
3133
*
@@ -60,6 +62,8 @@ public function __construct(
6062
}
6163

6264
/**
65+
* Create customer collection.
66+
*
6367
* @return void
6468
*/
6569
protected function _construct()
@@ -68,6 +72,8 @@ protected function _construct()
6872
}
6973

7074
/**
75+
* Create customer collection.
76+
*
7177
* @return Collection
7278
*/
7379
public function getCustomerCollection()
@@ -76,6 +82,8 @@ public function getCustomerCollection()
7682
}
7783

7884
/**
85+
* Load by param.
86+
*
7987
* @return $this
8088
*/
8189
public function loadByParam()
@@ -87,6 +95,8 @@ public function loadByParam()
8795
}
8896

8997
/**
98+
* Method for deleting customer from website.
99+
*
90100
* @param int $customerId
91101
* @param int $websiteId
92102
* @return $this

app/code/Magento/ProductAlert/Model/Stock.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
* @method \Magento\ProductAlert\Model\Stock setSendCount(int $value)
2525
* @method int getStatus()
2626
* @method \Magento\ProductAlert\Model\Stock setStatus(int $value)
27+
* @method int getStoreId()
28+
* @method \Magento\ProductAlert\Model\Stock setStoreId(int $value)
2729
*
2830
* @author Magento Core Team <[email protected]>
2931
*
@@ -58,6 +60,8 @@ public function __construct(
5860
}
5961

6062
/**
63+
* Class constructor.
64+
*
6165
* @return void
6266
*/
6367
protected function _construct()
@@ -66,6 +70,8 @@ protected function _construct()
6670
}
6771

6872
/**
73+
* Create customer collection.
74+
*
6975
* @return Collection
7076
*/
7177
public function getCustomerCollection()
@@ -74,6 +80,8 @@ public function getCustomerCollection()
7480
}
7581

7682
/**
83+
* Load by param.
84+
*
7785
* @return $this
7886
*/
7987
public function loadByParam()
@@ -85,6 +93,8 @@ public function loadByParam()
8593
}
8694

8795
/**
96+
* Method for deleting customer from website.
97+
*
8898
* @param int $customerId
8999
* @param int $websiteId
90100
* @return $this

0 commit comments

Comments
 (0)