I am counting associated products to show the the number of child items on grouped product in the category page. This is however very resource intensive. Is there a better way to show the count of the number of child products?
<?php $_productCollection = $block->getLoadedProductCollection(); foreach ($_productCollection as $_product): $associatedProductsCount = count($_product->getTypeInstance()->getAssociatedProducts($_product)); ?> <?php if($associatedProductsCount):?> <p><?php echo sprintf('%s Items Available', $associatedProductsCount)?></p> <?php endif;?> <?php endforeach; ?>
Edit I want to note that I went and tried the accepted answer in a small production environment v2.3.3 and it was about 10 times faster(I.E .0103 vs .00189 seconds) per call.