@@ -66,8 +66,6 @@ class ProductTotals
66
66
*/
67
67
private ResourceConnection $ resource ;
68
68
69
- private ?Collection $ parentsCollection = null ;
70
-
71
69
/**
72
70
* @param CollectionFactory $productCollectionFactory
73
71
* @param Builder $sqlBuilder
@@ -184,34 +182,32 @@ private function getProductCollection(string $conditions, bool $usePriceIndex =
184
182
* Get parent products that don't have stand-alone properties (e.g. price or special price)
185
183
*
186
184
* @param Collection $collection
187
- * @return Collection
185
+ * @return Collection|null
188
186
* @throws \Exception
189
187
*/
190
- private function getParentProductsCollection (Collection $ collection ): Collection
188
+ private function getParentProductsCollection (Collection $ collection ): ? Collection
191
189
{
192
- if (!$ this ->parentsCollection ) {
193
- $ ids = $ collection ->getAllIds ();
194
- $ linkField = $ this ->metadataPool ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class)
195
- ->getLinkField ();
196
- $ connection = $ this ->resource ->getConnection ();
197
- $ productIds = $ connection ->fetchCol (
198
- $ connection
199
- ->select ()
200
- ->from (['e ' => $ collection ->getTable ('catalog_product_entity ' )], ['link_table.parent_id ' ])
201
- ->joinInner (
202
- ['link_table ' => $ collection ->getTable ('catalog_product_super_link ' )],
203
- 'link_table.product_id = e. ' . $ linkField ,
204
- []
205
- )
206
- ->where ('link_table.product_id IN (?) ' , $ ids )
207
- );
208
-
209
- $ parentProducts = $ this ->productCollectionFactory ->create ();
190
+ $ parentProducts = $ this ->productCollectionFactory ->create ();
191
+ $ linkField = $ this ->metadataPool ->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class)
192
+ ->getLinkField ();
193
+ $ connection = $ this ->resource ->getConnection ();
194
+ $ productIds = $ connection ->fetchCol (
195
+ $ connection
196
+ ->select ()
197
+ ->from (['e ' => $ collection ->getTable ('catalog_product_entity ' )], ['link_table.parent_id ' ])
198
+ ->joinInner (
199
+ ['link_table ' => $ collection ->getTable ('catalog_product_super_link ' )],
200
+ 'link_table.product_id = e. ' . $ linkField ,
201
+ []
202
+ )
203
+ ->where ('link_table.product_id IN (?) ' , $ collection ->getAllIds ())
204
+ );
205
+ if ($ productIds ) {
210
206
$ parentProducts ->addIdFilter ($ productIds );
211
- $ this -> parentsCollection = $ parentProducts ;
207
+ return $ parentProducts ;
212
208
}
213
209
214
- return $ this -> parentsCollection ;
210
+ return null ;
215
211
}
216
212
217
213
/**
@@ -226,11 +222,13 @@ private function getEnabledCount(string $conditions): int
226
222
{
227
223
$ collection = $ this ->getProductCollection ($ conditions , true );
228
224
$ collection ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
225
+ $ count = $ collection ->getSize ();
226
+ if ($ collection ->getSize () && $ parentProducts = $ this ->getParentProductsCollection ($ collection )) {
227
+ $ parentProducts ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
228
+ $ count += $ parentProducts ->getSize ();
229
+ }
229
230
230
- $ parentProducts = $ this ->getParentProductsCollection ($ collection );
231
- $ parentProducts ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
232
-
233
- return $ collection ->getSize () + $ parentProducts ->getSize ();
231
+ return $ count ;
234
232
}
235
233
236
234
/**
@@ -244,11 +242,13 @@ private function getDisabledCount(string $conditions): int
244
242
{
245
243
$ collection = $ this ->getProductCollection ($ conditions , false );
246
244
$ collection ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED );
245
+ $ count = $ collection ->getSize ();
246
+ if ($ count && $ parentProducts = $ this ->getParentProductsCollection ($ collection )) {
247
+ $ parentProducts ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED );
248
+ $ count += $ parentProducts ->getSize ();
249
+ }
247
250
248
- $ parentProducts = $ this ->getParentProductsCollection ($ collection );
249
- $ parentProducts ->addAttributeToFilter ('status ' , Status::STATUS_DISABLED );
250
-
251
- return $ collection ->getSize () + $ parentProducts ->getSize ();
251
+ return $ count ;
252
252
}
253
253
254
254
/**
@@ -269,18 +269,20 @@ private function getNotVisibleCount(string $conditions): int
269
269
Visibility::VISIBILITY_IN_SEARCH
270
270
]
271
271
);
272
+ $ count = $ collection ->getSize ();
273
+ if ($ count && $ parentProducts = $ this ->getParentProductsCollection ($ collection )) {
274
+ $ parentProducts ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
275
+ $ parentProducts ->addAttributeToFilter (
276
+ 'visibility ' ,
277
+ [
278
+ Visibility::VISIBILITY_NOT_VISIBLE ,
279
+ Visibility::VISIBILITY_IN_SEARCH
280
+ ]
281
+ );
282
+ $ count += $ parentProducts ->getSize ();
283
+ }
272
284
273
- $ parentProducts = $ this ->getParentProductsCollection ($ collection );
274
- $ parentProducts ->addAttributeToFilter ('status ' , Status::STATUS_ENABLED );
275
- $ parentProducts ->addAttributeToFilter (
276
- 'visibility ' ,
277
- [
278
- Visibility::VISIBILITY_NOT_VISIBLE ,
279
- Visibility::VISIBILITY_IN_SEARCH
280
- ]
281
- );
282
-
283
- return $ collection ->getSize () + $ parentProducts ->getSize ();
285
+ return $ count ;
284
286
}
285
287
286
288
/**
0 commit comments