Skip to content

Commit b61ef7f

Browse files
committed
ACP2E-1361: Customer is not getting product price drop email for subsequent product price changes
1 parent 88745a8 commit b61ef7f

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
/**
2929
* Class for mailing Product Alerts
30+
*
31+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3032
*/
3133
class AlertProcessor
3234
{
@@ -139,6 +141,7 @@ public function process(string $alertType, array $customerIds, int $websiteId):
139141
* @param int $websiteId
140142
* @return array
141143
* @throws \Exception
144+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
142145
*/
143146
private function processAlerts(string $alertType, array $customerIds, int $websiteId): array
144147
{
@@ -160,6 +163,7 @@ private function processAlerts(string $alertType, array $customerIds, int $websi
160163
/** @var Website $website */
161164
$website = $this->storeManager->getWebsite($websiteId);
162165
$defaultStoreId = $website->getDefaultStore()->getId();
166+
$products = [];
163167

164168
/** @var Price|Stock $alert */
165169
foreach ($collection as $alert) {
@@ -174,7 +178,12 @@ private function processAlerts(string $alertType, array $customerIds, int $websi
174178
$customer = $this->customerRepository->getById($alert->getCustomerId());
175179
}
176180

177-
$product = $this->productRepository->getById($alert->getProductId(), false, $defaultStoreId);
181+
if (!isset($products[$alert->getProductId()])) {
182+
$product = $this->productRepository->getById($alert->getProductId(), false, $defaultStoreId, true);
183+
$products[$alert->getProductId()] = $product;
184+
} else {
185+
$product = $products[$alert->getProductId()];
186+
}
178187

179188
switch ($alertType) {
180189
case self::ALERT_TYPE_STOCK:

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

Lines changed: 41 additions & 5 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\App\Area;
@@ -16,18 +19,20 @@
1619
use Magento\Framework\Phrase\Renderer\Translate as PhraseRendererTranslate;
1720
use Magento\Framework\Phrase\RendererInterface;
1821
use Magento\Framework\Translate;
22+
use Magento\Store\Model\Store;
1923
use Magento\Store\Model\StoreRepository;
2024
use Magento\TestFramework\Helper\Bootstrap;
2125
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
2226
use Magento\TestFramework\ObjectManager;
2327
use PHPUnit\Framework\TestCase;
2428

2529
/**
26-
* Test for Product Alert observer
27-
*
28-
* @magentoAppIsolation enabled
29-
* @magentoAppArea frontend
30-
*/
30+
* Test for Product Alert observer
31+
*
32+
* @magentoAppIsolation enabled
33+
* @magentoAppArea frontend
34+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
35+
*/
3136
class AlertProcessorTest extends TestCase
3237
{
3338
/**
@@ -127,6 +132,37 @@ public function testProcessPortuguese()
127132
$this->assertStringContainsString(substr($expectedText, 0, 50), $messageContent);
128133
}
129134

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

0 commit comments

Comments
 (0)