Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Get all products from a product collection ignoring the set limits?

I want to iterate over all products in the product collection given in the block Mage_Catalog_Block_Product_List_Toolbar, ignoring the limits that were set before by "setPageSize()" and "setCurPage()". My approach looks like the following:

/** @var Mage_Catalog_Block_Product_List_Toolbar $this */
//...
$collection = $this->getCollection();
// Remove the LIMIT and OFFSET parts from the generated SQL query:
$collection->getSelect()->reset(Zend_Db_Select::LIMIT_COUNT);
$collection->getSelect()->reset(Zend_Db_Select::LIMIT_OFFSET);
// Reload the collection using the new SQL query:
$collection->load();
foreach($collection as $product)
{
    // ...
}
// ...

The problem is, that the collection seems not to be reloaded, so the limits that were set before are still there. What am I missing here? Is the collection locked or something so that I can't change it?

like image 440
Subsurf Avatar asked Aug 20 '12 16:08

Subsurf


1 Answers

The collection you use in the product list toolbar block, usually is already loaded before and set to the toolbar instance by Mage_Catalog_Block_Product_List::_beforeHtml().

Just resetting count and offset for the statement is not enough.

You additionally need to reset the properties

Varien_Data_Collection::_isCollectionLoaded
Varien_Data_Collection::_pageSize

This can be done by

$collection->clear();
$collection->setPageSize(false);

Insert these instructions between your reset's and the load and you should be fine.

like image 155
Jürgen Thelen Avatar answered Oct 15 '22 11:10

Jürgen Thelen