2
votes

In /app/design/frontend/default/[theme]/template/catalog/product/list.phtml you have the following snippet that loops through and displays the $_productCollection contents.

<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
...
<?php endif; ?>

What I'd like to know is how to have the $_productCollection sorted by product type. Ultimately what I want is to have configurable products show first, then simple products.

Help would be much appreciated.

Cheers!

1

1 Answers

2
votes

One of the things that makes product collections in category views such a pain to customize is the coupling between toolbar, layered navigation, actual list display, and even with the Mage_Review module, which explicitly loads the collection in an event observer!

At first it seems that you could use the typical collection utilities (e.g. addAttributeToSort()) to add your conditions, but you will find that this will not work at the template level, as the collection data is already loaded. I surmise that the collection getter method being named getLoadedProductCollection() is a hint from the core team.

The lazy-load implementation of Magento collections is such that once they have been load()ed (directly or indirectly), they will not reload (i.e. transact with the database) without explicitly being forced to do so.

So, you need to set your ordering parameters on the collection before it is loaded. Tracing through the getLoadedProductCollection() method gets one quickly to the Mage_Catalog_Block_Product_List::_getProductCollection() method, which is where the block determines if it is on a product page (by checking the registry, yuck) or not. Tracking the state of the collection through the call stack, it can be determined that it's not loaded until the block's _beforeToHtml() method. If you were to trace back from the output, you can see that the collection is explicitly loaded by the Mage_Review observer, which consumes the catalog_block_product_list_collection event (see Mage_Review_Model_Observer::catalogBlockProductCollectionBeforeToHtml()). That gives a starting point and endpoint.

I should hopefully have a solution later.