Skip to content

Commit 29cfed2

Browse files
committed
Merge branch 'ACP2E-1361' of https://github.com/magento-l3/magento2ce into PR01262023
2 parents 73e25d4 + e47b164 commit 29cfed2

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

app/code/Magento/ProductAlert/Model/Mailing/AlertProcessor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public function process(string $alertType, array $customerIds, int $websiteId):
155155
* @param int $websiteId
156156
* @return array
157157
* @throws \Exception
158+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
158159
*/
159160
private function processAlerts(string $alertType, array $customerIds, int $websiteId): array
160161
{
@@ -182,6 +183,7 @@ private function processAlerts(string $alertType, array $customerIds, int $websi
182183
/** @var Website $website */
183184
$website = $this->storeManager->getWebsite($websiteId);
184185
$defaultStoreId = $website->getDefaultStore()->getId();
186+
$products = [];
185187

186188
/** @var Price|Stock $alert */
187189
foreach ($collection as $alert) {
@@ -196,7 +198,12 @@ private function processAlerts(string $alertType, array $customerIds, int $websi
196198
$customer = $this->customerRepository->getById($alert->getCustomerId());
197199
}
198200

199-
$product = $this->productRepository->getById($alert->getProductId(), false, $defaultStoreId);
201+
if (!isset($products[$alert->getProductId()])) {
202+
$product = $this->productRepository->getById($alert->getProductId(), false, $defaultStoreId, true);
203+
$products[$alert->getProductId()] = $product;
204+
} else {
205+
$product = $products[$alert->getProductId()];
206+
}
200207

201208
switch ($alertType) {
202209
case self::ALERT_TYPE_STOCK:

dev/tests/integration/testsuite/Magento/ProductAlert/Model/Mailing/AlertProcessorTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Magento\ProductAlert\Model\Mailing;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\ProductFactory;
12+
use Magento\Catalog\Model\ResourceModel\Product as ProductResourceModel;
1013
use Magento\Customer\Api\AccountManagementInterface;
1114
use Magento\Customer\Model\Session;
1215
use Magento\Framework\Locale\Resolver;
@@ -18,6 +21,7 @@
1821
use Magento\Framework\Translate;
1922
use Magento\Framework\View\Design\ThemeInterface;
2023
use Magento\Framework\View\DesignInterface;
24+
use Magento\Store\Model\Store;
2125
use Magento\Store\Model\StoreRepository;
2226
use Magento\TestFramework\Helper\Bootstrap;
2327
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
@@ -136,6 +140,37 @@ public function testProcessPortuguese()
136140
$this->assertStringContainsString(substr($expectedText, 0, 50), $messageContent);
137141
}
138142

143+
/**
144+
* @magentoConfigFixture current_store catalog/productalert/allow_price 1
145+
* @magentoDataFixture Magento/ProductAlert/_files/product_alert.php
146+
*/
147+
public function testCustomerShouldGetEmailForEveryProductPriceDrop(): void
148+
{
149+
$this->processAlerts();
150+
151+
$this->assertStringContainsString(
152+
'$10.00',
153+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
154+
);
155+
156+
// Intentional: update product without using ProductRepository
157+
// to prevent changes from being cached on application level
158+
$product = $this->objectManager->get(ProductFactory::class)->create();
159+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
160+
$productResource = $this->objectManager->get(ProductResourceModel::class);
161+
$product->setStoreId(Store::DEFAULT_STORE_ID);
162+
$productResource->load($product, $productRepository->get('simple')->getId());
163+
$product->setPrice(5);
164+
$productResource->save($product);
165+
166+
$this->processAlerts();
167+
168+
$this->assertStringContainsString(
169+
'$5.00',
170+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
171+
);
172+
}
173+
139174
/**
140175
* Process price alerts
141176
*/

0 commit comments

Comments
 (0)