Currently used code for retrieving filtered product collection:
$products = Mage::getModel('catalog/product')
->setStoreId($storeId)
->getCollection()
->addAttributeToSort()
->addStoreFilter()
->addAttributeToFilter('category_id', array('in' => $rootCategoryId))
->addAttributeToFilter('type_id', 'configurable')
->addAttributeToFilter('sku', array('neq' => $attribute_set_id2))
->addAttributeToFilter('visibility', array('in' => array(4) ))
->addCategoryFilter(Mage::getModel('catalog/category')->load($categoryID));
$products->getSelect()->order(new Zend_Db_Expr('RAND()'));
Which I think is fine. Issue begins when I use following code for displaying products from previous collection as:
foreach($products as $product):
if ($counter == 6) { return $productArray; }
$fullProduct = Mage::getModel('catalog/product')->load($product->getId());
$images_obj = $this->helper('catalog/image')->init($fullProduct, 'small_image')->resize(252, 308);
$images[] = (string)$images_obj;
$productArray[$product->getId()]['image'] = (string)$images_obj;
$productArray[$product->getId()]['url'] = $fullProduct->getProductUrl();
$productArray[$product->getId()]['name'] = $fullProduct->getName();
$productArray[$product->getId()]['price'] = Mage::helper('core')->currency($fullProduct->getFinalPrice(),true);
$productArray[$product->getId()]['category'] = $fullProduct->getCategory();
$counter++;
endforeach;
Real performance killer here is:
$fullProduct = Mage::getModel('catalog/product')->load($product->getId());
Wondering is it possible to somehow store product IDs from collection and use later in foreach loop? Or any different workaround on this issue so script does not hit DB so harsh. Many thanks