@@ -151,58 +151,59 @@ private function processAlerts(string $alertType, array $customerIds, int $websi
151
151
$ email ->setWebsiteId ($ websiteId );
152
152
$ errors = [];
153
153
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 ;
163
154
/** @var Website $website */
164
155
$ 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
- }
180
156
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 );
187
163
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 );
196
166
} catch (\Exception $ e ) {
197
167
$ errors [] = $ e ->getMessage ();
168
+ continue ;
198
169
}
199
- }
200
170
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
+ }
206
207
}
207
208
}
208
209
@@ -227,25 +228,25 @@ private function validateAlertType(string $alertType): void
227
228
*
228
229
* @param string $alertType
229
230
* @param array $customerIds
230
- * @param int $websiteId
231
+ * @param int $storeId
231
232
* @return AbstractCollection
232
233
* @throws \InvalidArgumentException
233
234
*/
234
- private function getAlertCollection (string $ alertType , array $ customerIds , int $ websiteId ): AbstractCollection
235
+ private function getAlertCollection (string $ alertType , array $ customerIds , int $ storeId ): AbstractCollection
235
236
{
236
237
switch ($ alertType ) {
237
238
case self ::ALERT_TYPE_STOCK :
238
239
$ collection = $ this ->stockCollectionFactory ->create ();
239
240
$ collection ->addFieldToFilter ('customer_id ' , ['in ' => $ customerIds ])
240
- ->addWebsiteFilter ($ websiteId )
241
241
->addStatusFilter (0 )
242
+ ->addFilter ('store_id ' , $ storeId )
242
243
->setCustomerOrder ()
243
244
->addOrder ('product_id ' );
244
245
break ;
245
246
case self ::ALERT_TYPE_PRICE :
246
247
$ collection = $ this ->priceCollectionFactory ->create ();
247
248
$ collection ->addFieldToFilter ('customer_id ' , ['in ' => $ customerIds ])
248
- ->addWebsiteFilter ( $ websiteId )
249
+ ->addFilter ( ' store_id ' , $ storeId )
249
250
->setCustomerOrder ()
250
251
->addOrder ('product_id ' );
251
252
break ;
0 commit comments