cancel
Showing results for 
Search instead for 
Did you mean: 

Reduce response time on code

Reduce response time on code

Hi,

 

I am adding 2 screenshots and block of code. Please help me over this getting slow response after adding this block of code let me know the proper way or optimized block code

 

before.png

 

Block of Code which making this slower list.phtml file

 

 

<?php 
	$configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($product);
	$ids = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($_product->getId()); //get the children ids through a simple query
	$finalPrices = array();
	foreach ($ids[0] as $simpleProductId => $value) {
		$simpleproduct = Mage::getModel('catalog/product')->load($value);
		$finalpricing = $simpleproduct->getFinalPrice();
		if ($finalpricing != 0) {
			 $finalPrices[] = $finalpricing;   
		} 
	}
	$minprice = min($finalPrices);
	//echo $_product->getFinalPrice();
	//echo $_product->getPrice(); 
?> <span class="price">$<?php echo $minprice ?></span>

after.png

 

Tags (1)
4 REPLIES

Re: Reduce response time on code

You're loading product inside loop which is usually considered a Bad Idea. Every load() creates an additional query per product. I suggest you use Mage::getModel('catalog/product')->getCollection() instead (you need to add appropriate filters though). This generates a single query for all simple products involved and instead of loading them one by one you can just parse through the result set. It's way more efficent.

Tanel Raja

Re: Reduce response time on code

Hi Pronto,

 

You are wight but i want is to show lowest price of configurable product in list.phtml there are more than 200 product in this category and all of them are configurable product with simple product associated with 

 

This code is doing work for me i.e get the lowest price from simple products for me but some how due to loading queries again and again slows down the process

 

I had also tested this code in loop and also doing work but somehow slows down the site any one give me good code at least not slows down the process

 

<?php if($_product->getTypeId() == "configurable"): ?>
<?php $_configurable = $_product->getTypeInstance()->getUsedProductIds(); ?>
<?php echo "<pre>"; print_r($_configurable); echo "</pre>"; foreach ($_configurable as $_config): ?>
<?php $_simpleproduct = Mage::getModel('catalog/product')->load($_config); ?>
<?php echo $_simpleproduct->getPrice()//Magic php with a $_simpleproduct. ?>
<?php endforeach; ?>
<?php endif; ?>

Re: Reduce response time on code

This code is at least optimised it's slowing down but still going server response to 2.8s.

 

Did anyone able to give me more optimised code 

 

<?php 
if($_product->isconfigurable()) {
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); 
    $simple_collection = $conf->getUsedProductCollection()
                        ->addAttributeToSelect('*')
                        ->addFilterByRequiredOptions()
                        ->addAttributeToSort('Price', 'asc'); 

    foreach($simple_collection as $simple_product){ 
    echo '</br>Price: ' . Mage::helper('core')->currency($simple_product->getPrice()) . '</br></br>';
    //break;
    }   
}
?>

Re: Reduce response time on code

I'm pretty sure you don't want all the attributes ('*'). And you probably want to add additional conditions to skip products that you don't need, such as disabled products.

Tanel Raja