Skip to content

Instantly share code, notes, and snippets.

@Maksold
Last active Oct 25, 2018
Embed
What would you like to do?
#magento - get previous/next #category
<?php
/**
* Additional Methods for Mage Core Navigation
*
* @category Scandi
* @package Scandi_Extends
*/
class Scandi_Extends_Block_Catalog_Navigation extends Mage_Catalog_Block_Navigation
{
private function getCategoryByPositionOffset($offset)
{
$currentCategory = Mage::registry('current_category');
if ($currentCategory) {
// Get Parent category subCategories
$categories = Mage::getModel('catalog/category')->load($currentCategory->parent_id)->getChildrenCategories();
if (count($categories) > 0) {
// Create subCategories Id's array
$categoryPosIdArr = array();
foreach ($categories as $category) {
array_push($categoryPosIdArr, (int)$category->getId());
}
$categoryPos = array_search($currentCategory->getId(), $categoryPosIdArr) + $offset;
if (isset($categoryPosIdArr[$categoryPos])) {
$categoryId = $categoryPosIdArr[$categoryPos];
return Mage::getModel('catalog/category')->load($categoryId);
}
}
return false;
}
return false;
}
public function getNextCategory()
{
return $this->getCategoryByPositionOffset(1);
}
public function getPreviousCategory()
{
return $this->getCategoryByPositionOffset(-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment