cancel
Showing results for 
Search instead for 
Did you mean: 

Speeding Up QTY Select Dropdown for Grouped Products

Speeding Up QTY Select Dropdown for Grouped Products

Hi All,

 

We are implementing a Qty Select Dropdown on our products, but we are having a bit of trouble with our Grouped Products. We have a working solution, but it queries the entire inventory creating a pretty massive load time for our mobile customers.

 

Here is the current code:

 

<table class="data-table grouped-items-table" id="super-product-table">
    <col />
    <col />
    <col width="1" />
    <thead>
    </thead>
    <tbody>
    <?php if ($_hasAssociatedProducts): ?>
    <?php foreach ($_associatedProducts as $_item): ?>
        <?php $_finalPriceInclTax = $this->helper('tax')->getPrice($_item, $_item->getFinalPrice(), true) ?>
		<?php $_itemStock = Mage::getModel("cataloginventory/stock_item")->loadByProduct($_item); ?>
        <?php $_itemIncrement = $_itemStock->getQtyIncrements(); ?>
        <tr itemprop="offerDetails" itemscope itemtype="http://data-vocabulary.org/Offer">
            <span itemscope itemtype="http://schema.org/Product" itemref="#product_base">
        	<td class="item-image"><img itemprop="image" src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(38); ?>" width="38" alt="<?php echo $this->stripTags($this->getImageLabel($_item, 'small_image'), null, true) ?>" /></td>
            <td itemprop="name"><?php echo $this->escapeHtml($_item->getName()) ?></td>
            <?php if ($this->getCanShowProductPrice($_product)): ?>
                <td class="a-right">
                    <meta itemprop="price" content="<?php echo number_format($_item->getPrice(), 2);?>" />
					<meta itemprop="currency" content="<?php echo Mage::app()->getStore()->getCurrentCurrencyCode();?>" />
                    <?php if ($this->getCanShowProductPrice($_item)): ?>
                    <?php echo $this->getPriceHtml($_item, true) ?>
                    <?php echo $this->getTierPriceHtml($_item) ?>
                    <?php endif; ?>
                </td>
            <?php endif; ?>
            <?php if ($_product->isSaleable()): ?>
            <td class="a-center">
            <?php if ($_item->isSaleable()) : ?>
                <meta itemprop="availability" content="in_stock" />              
				<?php if ( $_itemIncrement > 1) :?>
                    <select class="input-text qty" name="qty" id="qty">
                        <?php $i = 0; ?>
                        <?php do { ?>
                            <option value="<?php echo $i?>">
                                <?php echo $i; ?>
                                <?php $i += $_itemIncrement; ?>
                            </option>
                        <?php } while ($i <= (int)$_itemStock->getMaxSaleQty()) ?>
                    </select>
				<?php else: ?>
                     <select class="input-text qty" name="qty" id="qty">
						<?php $i = 0; ?>
						<?php do { ?>
                            <option value="<?php echo $i?>">
								<?php echo $i; ?>
								<?php $i++; ?>
                            </option>
						<?php } while ($i <= (int)$_itemStock->getMaxSaleQty()) ?>
                    </select>
				<?php endif; ?>               
            <?php else: ?>
                <meta itemprop="availability" content="out_of_stock" />
                <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
            <?php endif; ?>
            </td>
            <?php endif; ?>
        </tr>
    <?php endforeach; ?>
    <?php else: ?>
       <tr>
           <td colspan="<?php if ($_product->isSaleable()): ?>4<?php else : ?>3<?php endif; ?>"><?php echo $this->__('No options of this product are available.') ?></td>
       </tr>
    <?php endif; ?>
    </tbody>
</table>

You can see that this code here:

 

<?php $_itemStock = Mage::getModel("cataloginventory/stock_item")->loadByProduct($_item); ?>
<?php $_itemIncrement = $_itemStock->getQtyIncrements(); ?>

Queries the database for each child product, creating a massive load on our servers.

 

Does anyone have an idea on a way to speed this up so that it doesn't query the database countless times?

 

Best Wishes.