Skip to content

Instantly share code, notes, and snippets.

@byjujohn
Created January 10, 2013 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byjujohn/4501573 to your computer and use it in GitHub Desktop.
Save byjujohn/4501573 to your computer and use it in GitHub Desktop.
Magento : Product navigation (getting previous and next items)
<?php
/**
* Determine the previous/next link and link to current category
*/
$_ccat = $this->helper('catalog/data')->getCategory();
$ppos = $_ccat->getProductsPosition();
$current_pid = $this->helper('catalog/data')->getProduct()->getId();
// build array from products positions
$plist = array();
foreach ($ppos as $pid => $pos) {
$plist[] = $pid;
}
$curpos = array_search($current_pid, $plist);
// get link for prev product
$previd = isset($plist[$curpos+1])? $plist[$curpos+1] : $current_pid;
$product = Mage::getModel('catalog/product')->load($previd);
$prevpos = $curpos;
while (!$product->isVisibleInCatalog()) {
$prevpos += 1;
$nextid = isset($plist[$prevpos])? $plist[$prevpos] : $current_pid;
$product = Mage::getModel('catalog/product')->load($nextid);
}
$prev_url = $product->getProductUrl();
// get link for next product
$nextid = isset($plist[$curpos-1])? $plist[$curpos-1] : $current_pid;
$product = Mage::getModel('catalog/product')->load($nextid);
$nextpos = $curpos;
while (!$product->isVisibleInCatalog()) {
$nextpos -= 1;
$nextid = isset($plist[$nextpos])? $plist[$nextpos] : $current_pid;
$product = Mage::getModel('catalog/product')->load($nextid);
}
$next_url = $product->getProductUrl();
// get link for current category
$more_url = $_ccat->getUrl();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment