Created
January 10, 2013 12:10
-
-
Save byjujohn/4501573 to your computer and use it in GitHub Desktop.
Magento : Product navigation (getting previous and next items)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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