Magento – magento sort by price not working fine

magento-1.9pricesort

sort by price is not working in magento 1.9

my site has just configurable products only .
when i checked code it was using below code to load products

$_productCollection=$this->getLoadedProductCollection();

so i though of temporary fix (below code) , but that code work fine for just single page , issue is still there when user navigates to next page. so i need way to fix this bug . Does any one have idea page where getLoadedProductCollection is defined . So i can add sort there to fix issue

foreach($_productCollection as $mprod){
    $products=Mage::getModel("catalog/product")->loadByAttribute('sku',$mprod->getSku());
    $obj = Mage::getModel('catalog/product');
    $product = $obj->load($products->getData('default_configuration_id')); echo " ";
    $mysortprice[(int)$product->getPrice()] =  $mprod;
}
$sortorder = (string)$this->getRequest()->getParam('dir');
$sorttype = (string)$this->getRequest()->getParam('order');

if($sortorder=='asc' && $sorttype=='price'){
    ksort($mysortprice);
}else if($sortorder=='desc' && $sorttype=='price'){
        krsort($mysortprice);
}

Best Answer

1) Sorting by price should work perfectly for any kind of products if you have Price Attribute chosed if Available Sort Options (on Category edit page) and if you have Price Index built well.

Just try to add "?dir=desc&order=price" to your category url and see if it will change sorting

2) But if you would like to apply your custom sorting anyway - you can use Observer method, which is described here

3) But, call for "loadByAttribute" for all products in the collection is very heavy. Instead of it you need to use next code:

$products = $observer->getCollection();
$products->addFinalPrice();
$products->getSelect()
    ->order('price_index.final_price', 'asc');
Related Topic