Skip to content

Instantly share code, notes, and snippets.

@Maksold
Last active October 25, 2018 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maksold/4123810 to your computer and use it in GitHub Desktop.
Save Maksold/4123810 to your computer and use it in GitHub Desktop.
#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