Skip to content

Commit a733b11

Browse files
committed
Merge remote-tracking branch 'tier4/ACP2E-3336' into PR-10-04-24
2 parents 581b7ef + c397d78 commit a733b11

File tree

3 files changed

+125
-48
lines changed

3 files changed

+125
-48
lines changed

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

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -151,58 +151,59 @@ private function processAlerts(string $alertType, array $customerIds, int $websi
151151
$email->setWebsiteId($websiteId);
152152
$errors = [];
153153

154-
try {
155-
$collection = $this->getAlertCollection($alertType, $customerIds, $websiteId);
156-
} catch (\Exception $e) {
157-
$errors[] = $e->getMessage();
158-
return $errors;
159-
}
160-
161-
/** @var CustomerInterface $customer */
162-
$customer = null;
163154
/** @var Website $website */
164155
$website = $this->storeManager->getWebsite($websiteId);
165-
$defaultStoreId = $website->getDefaultStore()->getId();
166-
$products = [];
167-
168-
/** @var Price|Stock $alert */
169-
foreach ($collection as $alert) {
170-
try {
171-
if ($alert->getStoreId()) {
172-
$email->setStoreId($alert->getStoreId());
173-
}
174-
if ($customer === null) {
175-
$customer = $this->customerRepository->getById($alert->getCustomerId());
176-
} elseif ((int)$customer->getId() !== (int)$alert->getCustomerId()) {
177-
$this->sendEmail($customer, $email);
178-
$customer = $this->customerRepository->getById($alert->getCustomerId());
179-
}
180156

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-
}
157+
foreach ($website->getStores() as $store) {
158+
/** @var CustomerInterface $customer */
159+
$customer = null;
160+
$products = [];
161+
$storeId = (int)$store->getId();
162+
$email->setStoreId($storeId);
187163

188-
switch ($alertType) {
189-
case self::ALERT_TYPE_STOCK:
190-
$this->saveStockAlert($alert, $product, $website, $email);
191-
break;
192-
case self::ALERT_TYPE_PRICE:
193-
$this->savePriceAlert($alert, $product, $customer, $email);
194-
break;
195-
}
164+
try {
165+
$collection = $this->getAlertCollection($alertType, $customerIds, $storeId);
196166
} catch (\Exception $e) {
197167
$errors[] = $e->getMessage();
168+
continue;
198169
}
199-
}
200170

201-
if ($customer !== null) {
202-
try {
203-
$this->sendEmail($customer, $email);
204-
} catch (\Exception $e) {
205-
$errors[] = $e->getMessage();
171+
/** @var Price|Stock $alert */
172+
foreach ($collection as $alert) {
173+
try {
174+
if ($customer === null) {
175+
$customer = $this->customerRepository->getById($alert->getCustomerId());
176+
} elseif ((int)$customer->getId() !== (int)$alert->getCustomerId()) {
177+
$this->sendEmail($customer, $email);
178+
$customer = $this->customerRepository->getById($alert->getCustomerId());
179+
}
180+
181+
if (!isset($products[$alert->getProductId()])) {
182+
$product = $this->productRepository->getById($alert->getProductId(), false, $storeId, true);
183+
$products[$alert->getProductId()] = $product;
184+
} else {
185+
$product = $products[$alert->getProductId()];
186+
}
187+
188+
switch ($alertType) {
189+
case self::ALERT_TYPE_STOCK:
190+
$this->saveStockAlert($alert, $product, $website, $email);
191+
break;
192+
case self::ALERT_TYPE_PRICE:
193+
$this->savePriceAlert($alert, $product, $customer, $email);
194+
break;
195+
}
196+
} catch (\Exception $e) {
197+
$errors[] = $e->getMessage();
198+
}
199+
}
200+
201+
if ($customer !== null) {
202+
try {
203+
$this->sendEmail($customer, $email);
204+
} catch (\Exception $e) {
205+
$errors[] = $e->getMessage();
206+
}
206207
}
207208
}
208209

@@ -227,25 +228,25 @@ private function validateAlertType(string $alertType): void
227228
*
228229
* @param string $alertType
229230
* @param array $customerIds
230-
* @param int $websiteId
231+
* @param int $storeId
231232
* @return AbstractCollection
232233
* @throws \InvalidArgumentException
233234
*/
234-
private function getAlertCollection(string $alertType, array $customerIds, int $websiteId): AbstractCollection
235+
private function getAlertCollection(string $alertType, array $customerIds, int $storeId): AbstractCollection
235236
{
236237
switch ($alertType) {
237238
case self::ALERT_TYPE_STOCK:
238239
$collection = $this->stockCollectionFactory->create();
239240
$collection->addFieldToFilter('customer_id', ['in' => $customerIds])
240-
->addWebsiteFilter($websiteId)
241241
->addStatusFilter(0)
242+
->addFilter('store_id', $storeId)
242243
->setCustomerOrder()
243244
->addOrder('product_id');
244245
break;
245246
case self::ALERT_TYPE_PRICE:
246247
$collection = $this->priceCollectionFactory->create();
247248
$collection->addFieldToFilter('customer_id', ['in' => $customerIds])
248-
->addWebsiteFilter($websiteId)
249+
->addFilter('store_id', $storeId)
249250
->setCustomerOrder()
250251
->addOrder('product_id');
251252
break;

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,78 @@ public function testValidateCurrentTheme()
243243
);
244244
}
245245

246+
#[
247+
DbIsolation(false),
248+
DataFixture(StoreFixture::class, ['code' => 'pt_br_store'], 'store2'),
249+
DataFixture(CustomerFixture::class, as: 'customer'),
250+
DataFixture(ProductFixture::class, as: 'product'),
251+
DataFixture(
252+
PriceAlertFixture::class,
253+
[
254+
'customer_id' => '$customer.id$',
255+
'product_id' => '$product.id$',
256+
'store_id' => '1',
257+
]
258+
),
259+
DataFixture(
260+
PriceAlertFixture::class,
261+
[
262+
'customer_id' => '$customer.id$',
263+
'product_id' => '$product.id$',
264+
'store_id' => '$store2.id$',
265+
]
266+
),
267+
DataFixture(
268+
TranslationFixture::class,
269+
[
270+
'string' => 'Price change alert! We wanted you to know that prices have changed for these products:',
271+
'translate' => 'Alerte changement de prix! Nous voulions que vous sachiez' .
272+
' que les prix ont changé pour ces produits:',
273+
'locale' => 'fr_FR',
274+
],
275+
'frTxt'
276+
),
277+
DataFixture(
278+
TranslationFixture::class,
279+
[
280+
'string' => 'Price change alert! We wanted you to know that prices have changed for these products:',
281+
'translate' => 'Alerta de mudanca de preco! Queriamos que voce soubesse' .
282+
' que os precos mudaram para esses produtos:',
283+
'locale' => 'pt_BR',
284+
],
285+
'ptTxt'
286+
),
287+
Config('catalog/productalert/allow_price', 1),
288+
Config('general/locale/code', 'fr_FR', ScopeInterface::SCOPE_STORE, 'default'),
289+
Config('general/locale/code', 'pt_BR', ScopeInterface::SCOPE_STORE, 'pt_br_store'),
290+
]
291+
public function testEmailShouldBeTranslatedToStoreViewLanguage()
292+
{
293+
$customerId = (int)$this->fixtures->get('customer')->getId();
294+
295+
$frMailSent = false;
296+
$ptMailSent = false;
297+
$this->transportBuilder->setOnMessageSentCallback(
298+
function ($message) use (&$frMailSent, &$ptMailSent) {
299+
$messageContent = $message->getBody()->getParts()[0]->getRawContent();
300+
$frTxt = $this->fixtures->get('frTxt')->getTranslate();
301+
$ptTxt = $this->fixtures->get('ptTxt')->getTranslate();
302+
303+
if (str_contains($messageContent, $frTxt)) {
304+
$frMailSent = true;
305+
}
306+
307+
if (str_contains($messageContent, $ptTxt)) {
308+
$ptMailSent = true;
309+
}
310+
}
311+
);
312+
313+
$this->processAlerts($customerId);
314+
$this->assertTrue($frMailSent);
315+
$this->assertTrue($ptMailSent);
316+
}
317+
246318
/**
247319
* @param int $customerId
248320
* @param int $websiteId

dev/tests/integration/testsuite/Magento/ProductAlert/_files/product_alert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
$product->getPrice()+1
3030
)->setWebsiteId(
3131
1
32+
)->setStoreId(
33+
1
3234
);
3335
$price->save();
3436

@@ -39,5 +41,7 @@
3941
$product->getId()
4042
)->setWebsiteId(
4143
1
44+
)->setStoreId(
45+
1
4246
);
4347
$stock->save();

0 commit comments

Comments
 (0)