1.9.3.1 - My catalog setup is using configurable products with associated simple products. My category pages show the configurable and I'd like for the 'Add to Cart' text to read 'Only X Left' if all of the simple products for that product combined quantity is below a specific threshold. I got this working, but it puts a heavy strain on page load time whenever a cache refresh is triggered. I lazy load the category pages to begin with and this recursive manual tally of simple products multiplied by about 100 configurable products per category creates load times over 30 seconds in some cases. Is there a better method to retrieve this value (does it exist already somewhere?) and display it than how I'm accomplishing?
/app/design/frontend/mytheme/default/template/catalog/product/list.phtml
<?php $btnText = 'Add to Cart' ?>
<?php $sku = Mage::getModel('catalog/product')->load($_product->getId())->getSku(); ?>
<?php $total = 0; ?>
<?php $ids = $_product->getTypeInstance()->getUsedProductIds(); ?>
<?php foreach ($ids as $id) : ?>
<?php $simpleproduct = Mage::getModel('catalog/product')->load($id); ?>
<?php $total = $total + (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty(); ?>
<?php endforeach; ?>
<?php if ($total < 6) { ?>
<?php $btnText = 'Only ' . $total .' left!' ?>
<?php } ?>