Tuesday 8 September 2015

Latest products come first in category magento


Latest products come first in category magento

In magento there is by default product position sort order ASC so the newly added product goes to last and old product remains first. But people wants to latest products first when they visit any category of the site. Here i have used small trick to display newly product in DESC order.

Go to app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php

Find the below function in this file

public function setCollection($collection)
{
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
   
    $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
   

    return $this;
}


And replace this to below one

public function setCollection($collection)
{
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    if ($this->getCurrentOrder()) {
      if(($this->getCurrentOrder())=='position'){
          $this->_collection->setOrder('entity_id','desc');
      }
      else {
       $this->_collection->setOrder($this->getCurrentOrder(),$this->getCurrentDirection());
      }
    }
    return $this;
}

No comments: