1

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.

3 Answers 3

3

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!

3
  • I'm still looking to improve this code so that it doesn't include disabled products. Nov 23, 2016 at 20:23
  • This works for Magento 2 also
    – Ajith
    Mar 5, 2020 at 10:37
  • @Ajith, who said it didn't ;) Mar 6, 2020 at 14:26
1
$product_confi = Mage::getModel('catalog/product')->load('1100'); //configure product id
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product_confi);

echo count($childProducts);

it will print total associate product associate with configure product.

if you want to get only enabled product than use this code

<?php
require_once('app/Mage.php');
Mage::app();
$product = Mage::getModel('catalog/product')->load(418); 
                    $childProducts = Mage::getModel('catalog/product_type_configurable')
                                    ->getUsedProducts(null,$product);
$count=0;   
foreach($childProducts as $child) 
{
    $product=Mage::getModel('catalog/product')->load($child->getId());
        echo $product->getId(). '-'.$product->getStatus();
        if($product->getStatus()==1)
        {
            $count++;
        }
        echo '<br>';
}

echo 'total enable product is '.$count;


?>
17
  • This is working great, thanks! Is there any performance hit by loading product_type_configurable? Sep 20, 2016 at 17:14
  • no. its only to load child product Sep 21, 2016 at 4:43
  • This is working good, except for the fact that it still counts associated products that have been disabled. How can we narrow it down to only enabled associated products? Sep 23, 2016 at 19:31
  • you use foreach loop for this. and check that product is enable or not Sep 24, 2016 at 4:01
  • I did loop through the array to check the status. However, the count is unchanged by disabled products. It seems the issue comes from passing "null" as the first parameter of getUsedProducts: stackoverflow.com/a/6584928/3254362 I tried adding the code as shown here with no luck: magento.stackexchange.com/a/68807/24331 Sep 26, 2016 at 20:52
0

I use this code for Tier price condition in magento and it's working fine showing price on simple products count in configurable product.

<?php                               
                                $childIds = $_product->getTypeInstance(true)->getChildrenIds($_product->getId(), false); //$_product is defined in the loop
                                $colors = count($childIds[0]); //getChildrenIds() returns an encapsulated array
                                if ($colors == 2) {                                 
                                    echo $this->getPriceHtml($_product, true);
                                } else { ?>                                     
                                    <span class="price-label" style="color:#03c069; margin-bottom:0px; display:none;"><?php echo $this->__('Price') ?></span>
                                    <span class="price" style="color:#113b80; margin-top: 6px;"> <?php echo $_coreHelper->currency (Min($allTier), true, true); ?> </span>
                            <?php } ?>
1
  • This is pretty much the same as my selected answer except you are using it to output price (not what I need) instead of the count. The only problem with my (and your) code is that disabled products are included in the count. I need a way so that the disabled products aren't included. Jul 15, 2017 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.