Search code examples
magento

How to sort products


How to sort by position products.

I am making a slider and a want products to be sorted on slider. I want to use build in sorting like best match, which is on category -> product tab

Here is how I am doing but won't work:

$product_collection = Mage::getModel('catalog/category')->getProductCollection()->setOrder('position','ASC');
    $selected = 0;
    foreach($product_collection as $product) {
        $full_product = Mage::getModel('catalog/product')->load($product->getId());
        if($full_product->getTypeId() === 'configurable'){ ?>
        <li>
              <img>
        </li>
<? } 
} ?> 

Solution

  • You need to load a category in the category model before pulling out its product collection

    $product_collection = Mage::getModel('catalog/category')->getProductCollection()->setOrder('position','ASC'); 
    

    should be replaced by

    $product_collection = Mage::getModel('catalog/category')->load($somecategoryid)->getProductCollection()->setOrder('position','ASC');