Adding NEXT button on Magento product view page

/app/design/frontend/base/default/template/catalog/product/view.phtml

 <?php

$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
$prev_url = $next_url = $url = $_product->getProductUrl();

if ($this->helper('catalog/data')->getCategory()) {
$category = $this->helper('catalog/data')->getCategory();
} else {
$_ccats = $this->helper('catalog/data')->getProduct()->getCategoryIds();
$category = Mage::getModel('catalog/category')->load($_ccats[0]);
}

$children = $category->getProductCollection();
$_count = is_array($children) ? count($children) : $children->count();
if ($_count) {
foreach ($children as $product) {
$plist[] = $product->getId();
}

/**
* Determine the previous/next link and link to current category
*/
$current_pid  = $this->helper('catalog/data')->getProduct()->getId();
$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();
}
?>
<?php if ($url <> $prev_url):?>
<a href="<?php echo $prev_url; ?>">NEXT</a>
<?php endif; ?> 
  • dudu

    Thank you,very helpful tips. Need change some codes,
    ——–add these——–
    <?php if ($url $next_url):?>
    <a href="”>prevoius< / a >

    ——–before these——–
    <?php if ($url $prev_url):?>
    <a href="”>NEXT < / a >

    so you can see both quick links.

  • Ryan

    This code works great!

    dudu, I added your code to create the PREV button but how do I NOT include the PREV button on the first item?

    As the NEXT button is not visible on the last item.

    thanks

  • admin

    @Ryan
    you can try it.
    < ? php if ($url <> $next_url): ? >
    < a href="< ? php echo $next_url;" >PRV< / a >
    < ? php endif; ? >

  • John Alif

    where i post it ?

  • http://www.facebook.com/praful.rajput Praful Rajput

    Very Nice Extension/Script!!!!