0

I have looked all over the internet but I could find only Next and Previous for products, but I want to have it at the bottom of category page where all the products (list.phtml) are listed.

I have attached a sample below. The example is from 25,000 - 50,000 Gallons category so before it is 700 - 25,000 Gallons category and 50,000 - 100,000 Gallons after it.

Next and Previous Category

Any help will be highly appreciated.

2 Answers 2

1

assuming that the prev/next categories have the same parent as the current category you can do this:

$currentCategory = Mage::registry('current_category');
$prev = null;
$next = null;
if ($currentCategory) {
    $storeId = Mage::app()->getStore()->getId();
    $parentId = $currentCategory->getParentId();
    $position = $currentCategory->getPosition();
    $prevCollection = Mage::getModel('catalog/category')->getCollection()
        ->setStoreId($storeId)
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('parent_id', $parentId) //filter by same parent id
        ->addAttributeToFilter('position', array('lt' => $position)) //get only categories with position smaller than current one
        ->addAttributeToSort('position', 'DESC') //sort by position descending
        ->setPageSize(1) //limit query to 1 item
        ->setCurPage(1);
    $firstElem = $prevCollection->getFirstItem(); 
    if ($firstElem) {
        $prev = $firstElem;
    }

    $nextCollection = Mage::getModel('catalog/category')->getCollection()
        ->setStoreId($storeId)
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('parent_id', $parentId)
        ->addAttributeToFilter('position', array('gt' => $position))
        ->addAttributeToSort('position', 'ASC')
        ->setPageSize(1)
        ->setCurPage(1);
    $firstElem = $nextCollection->getFirstItem(); 
    if ($firstElem) {
        $next = $firstElem;
    }
}

Then you can use this:

<?php if ($prev) : ?>
    <a href="<?php echo $prev->getUrl();?>">
         <?php echo $this->__('Back to %s', $prev->getName())?>
    </a>
<?php endif;?>

<?php if ($next) : ?>
    <a href="<?php echo $next->getUrl();?>">
         <?php echo $this->__('Continue to %s', $next->getName())?>
    </a>
<?php endif;?>
3
  • Both Next and Previous links shows Previous one.
    – Kevin S
    Mar 18, 2016 at 9:28
  • @KevinS. Sorry. I had a typo but Prashant fixed it. Try getting the code again.
    – Marius
    Mar 18, 2016 at 9:29
  • I wanted to Thank you but it is considered as spam here. :)
    – Kevin S
    Mar 18, 2016 at 9:42
0

Below code will helps you

<?php
$currentCategory = Mage::registry('current_category');
$categoryModel = Mage::getModel('catalog/category');
$parentId = $currentCategory->getParentId();

if ( $parentId)){
   $children = $categoryModel->getCategories($parentId); //get all children of parent category of current category
    $catIds = array();
    foreach ($children as $category) {
        $catIds[] =  $category->getId();        
    }
    $key = array_search($currentCategory->getId(), $catIds);

        if($catIds[$key-1]){            
            $previousCategory = Mage::getModel('catalog/category')->load($catIds[$key-1]);
            echo "<a href=".$previousCategory->getUrl($previousCategory)." >".$previousCategory->getName()."</a>";
        }
        if($catIds[$key+1]){
            $nextCategory = Mage::getModel('catalog/category')->load($catIds[$key+1]);
            echo "<a href=".$nextCategory->getUrl($nextCategory)." >".$nextCategory->getName()."</a>";
        }

}
2
  • 1
    this might work, but is there a need to load all the child categories just to get the previous and next categories and load the full categories? This will impact performance.
    – Marius
    Mar 18, 2016 at 9:07
  • this works, but it is really slow if you have 100+ categories Jan 31, 2017 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.