0
votes

This is for use on catalog category pages. The default getLoadedProductCollection method does not work properly in all cases, so I needed to do this.

I've been having trouble adding the price sorting below. By removing the addCategoryFilter, the products are properly sorted by price. By removing the price sorting method and keeping the addCategoryFilter, the category shows unsorted.

I tried the following to use join to filter the categories out and this didn't do anything:
http://magento.stackexchange.com/questions/7094/filter-product-collection-by-multiple-categories

$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCatId= $category->getId();
$category_model = Mage::getModel('catalog/category')->load($currentCatId);
$_productCollection = Mage::getModel('catalog/product')
  ->getCollection()
  ->addAttributeToSelect("*")
  ->addAttributeToSort('price', Varien_Data_Collection::SORT_ORDER_DESC)
  ->addStoreFilter(Mage::app()->getStore()->getId())
  ->addAttributeToFilter('status',1)
  ->setVisibility(
       Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds() )
  ->addCategoryFilter($category_model)
  ->load();

Any help would be appreciated. I'm not quite understanding why this isn't working properly.

P.S. Instead of addAttributeToSort, I have also tried the setOrder method to no avail.

1

1 Answers

0
votes

Instead of addAttributeToSort(), why don't you try setOrder()?

Something like this:

$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCatId= $category->getId();
$category_model = Mage::getModel('catalog/category')->load($currentCatId);
$_productCollection = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSelect("*")
    ->addStoreFilter(Mage::app()->getStore()->getId())
    ->setOrder('price', Varien_Data_Collection::SORT_ORDER_DESC)
    ->addAttributeToFilter('status',1)
    ->setVisibility(
         Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds() )
    ->addCategoryFilter($category_model)
    ->load();

Hope it helps