2

I have had to make some changes to the catalog page (list.phtml) in Magento, everything is fine except for the 'Sort By' name, position etc...

Here is my code:

$_productCollection= Mage::getModel('catalog/product')->getCollection()
                     ->addAttributeToSelect('*')
                     ->addStoreFilter()
                     ->setPage(Mage::getBlockSingleton('page/html_pager')->getCurrentPage(), $limit)
                     ->setPageSize( $limit )
                     ->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())
                     ->load();

There is something wrong here clearly as nothing happens when trying to sort the results bu name, position etc!

Obviously there is something wrong with this line:

->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())

I am also having a problem with Simple products that are set to not show individually are showing! I have missed something from there also.

If someone could steer me towards the correct functions/syntax that would be great!

  • 1
    `Mage::log($this->getCurrentOrder()); Mage::log($this->getCurrentDirection());` - what does it tell you? – benmarks Sep 07 '12 at 14:22

1 Answers1

6

I solved the ordering issue!

->setOrder(Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentOrder(), Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentDirection())

And products set to not show individually:

->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())

So the full code for anyone else:

if( $this->getMode()!='grid' ) {
    $limit = Mage::getStoreConfig('catalog/frontend/list_per_page'); 
}
else {
    $limit = Mage::getStoreConfig('catalog/frontend/grid_per_page');
}   
  $_productCollection= Mage::getModel('catalog/product')->getCollection()
                        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
                         ->addAttributeToSelect('sku_base')
                         ->addStoreFilter(Mage::app()->getStore()->getId())
                         ->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds())
                         ->setPage(Mage::getBlockSingleton('page/html_pager')->getCurrentPage(), $limit)
                         ->setPageSize( $limit )
                         ->setOrder(Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentOrder(), Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentDirection())
                         ->load();
  • 1
    Might i Just add: if user selects show x per page it will not work without this: if( Mage::app()->getRequest()->getParam('limit') ) $limit = Mage::app()->getRequest()->getParam('limit'); – Nathan Fitzgerald - Fitzgenius Sep 10 '12 at 09:10
  • Thanks, but also need check via PARAMS in URL becouse Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentOrder() set when created Toolbar – Alex Feb 15 '17 at 14:39