Question

I came across this block of code in an extension that loops through child products and attempts to return their current stock qty and the child products name:

<?php foreach ($this->getChildProducts() as $childProduct) : ?>
        <?php $childProductStockQty = $this-> getProductStockQty($childProduct);?>
        <?php if ($childProductStockQty > 0) : ?>
            <tr>
                <td><?php echo $childProduct->getName() ?></td>
                <td class="a-center"><?php echo $childProductStockQty ?></td>
            </tr>
        <?php endif ?>
    <?php endforeach ?>

In CE 1.9.0.1 this worked perfectly, returning both product names and stock quantities as expected. In CE 1.9.1, it will return the correct stock quantities, but the name returns null.

My question isn't so much 'what's wrong with this extension?', I've sent a question to the developer as well. I'm wondering if any of you are aware if there are changes in CE 1.9.1 in how to retrieve a child product name.

Reading the code down the Mage/CatalogInventory/Block/Stockqty rabbit hole hasn't shed any light that I can detect.

Any and all advice is appreciated.

Was it helpful?

Solution

Thats interesting that name is returning null. Hunting through the code it ends up in getUsedProducts that should Retrieve array of "subproducts" and returns a collection of products.

There are no changes that I can see when comparing a local repo from 1.9.0.1 to 1.9.1 so its a slight mystery. Even stranger that you are being returned stock but name is null.. Both should be present in the model as they are required fields. Perhaps the module does some additional modifications to the collection that is effecting core behaviour.

But to answer the question. I cant see anything that has changed between these two versions.

OTHER TIPS

I solve this with this line

<?php $childProduct = Mage::getModel('catalog/product')->load($childProduct->getId()); ?>

So I get all the childProduct info, and now the getName gets what we expect.

I hope this helps! not the best way, but works.

First, do you even have valid product objects in the loop? i.e. Is $childProduct->getId() retuning an ID or null? If so, something broke when you upgraded. Please note that $this->getProductStockQty() and $childProduct->getName() are not the same method. So, they don't neccessarily have to mutually work together or fail together.

Second, you would need to examine the respective block's getChildProducts() method. From there, you can trace how the collection is put together. It might be be because the collection (for whatever reason after you upgraded) doesn't include the name field in its addAttributeToFilter() filter.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top