0

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.

1

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!

  • I'm still looking to improve this code so that it doesn't include disabled products. – Michael Yaeger Nov 23 '16 at 20:23
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;


?>
  • This is working great, thanks! Is there any performance hit by loading product_type_configurable? – Michael Yaeger Sep 20 '16 at 17:14
  • no. its only to load child product – Jigs Parmar Sep 21 '16 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? – Michael Yaeger Sep 23 '16 at 19:31
  • you use foreach loop for this. and check that product is enable or not – Jigs Parmar Sep 24 '16 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 – Michael Yaeger Sep 26 '16 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 } ?>
  • 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. – Michael Yaeger Jul 15 '17 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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