Count Associated Products for Configurable Products in Magento 1.9

associated-productsconfigurable-productmagento-1.9

Is it possible to get a count of the total number of associated products assigned to a configurable product?

I have tried using the following functions:

  • getConfigurableAttributesAsArray()
  • getProductCount()
  • count()

However, I've only seen this working when applied to categories, not configurable products. While I do want to output the count in list.phtml, I want to output it per product, not for the entire category.

Best Answer

While the approach offered by jigs parmar does work, it is a HUGE performance hit if used in a loop! A much better approach is:

$childIds = $_product->getTypeInstance(true)->getChildrenIds($_product->getId(), false); //$_product is defined in the loop
$colors = count($childIds[0]); //getChildrenIds() returns an encapsulated array
echo $colors.' Color';
if ($colors != 1) echo 's'; //make "Color" plural

The reason is that getUsedProducts() returns every simple product along with every attribute of those products. In a loop, that took forever!

Since I only needed the number of products, I could use getChildrenIds() which only returns the ID of the simple products--much lighter code!

Related Topic