Display Previous Product & Next Product Links on Product view Page In Magento



Recently, My one client wants to add a Previous and Next Product links on the Product Description page, so that user can navigate the every products of the Individual categories without clicking on the categories and then individual products again and again to see the whole product description. To do this I have done research on the web and finally find out some good solutions over there and here I am sharing it on the web. You can do it easily by following these steps given below:

For displaying the Previous & Next product links after the Product Name in product view page. Something like that in the below layout:

To implment the Next and Previous product links on the product listing page, open the view.phtml file in the editor of your choice.

For the view.phtml, navigate to the app/design/frontend/default/your_theme_name/template/catalog/product/view.phtml or navigate to the app/design/frontend/base/default/template/catalog/product/view.phtml and insert the code, where you want to display the Previous and Next product link in it.

Suppose you want to display the Previous and Next product link below the product name find this code block

<div>
<h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
</div>

Then insert the following code.

<?php // Previous and Next product links in product page

$_products = $this->getProduct();
if(!$_products->getCategoryIds())
return; // it will not display the Previous and Next if there no product in any category

$cat_ids = $_products->getCategoryIds();
$cat = Mage::getModel('catalog/category')->load( $cat_ids[0] );

$order = Mage::getStoreConfig('catalog/frontend/default_sort_by');
$direction = 'asc'; // asc or desc

$category_product = $cat->getProductCollection()->addAttributeToSort($order, $direction);
$category_product->addAttributeToFilter('status',1);
$category_product->addAttributeToFilter('visibility',4);

$cat_prod_ids = $category_product->getAllIds(); // get all products from the category
$_product_id = $_products->getId();

$_pos = array_search($_product_id, $cat_prod_ids); // get position of current product
$_next_pos = $_pos+1;
$_prev_pos = $_pos-1;

// load the next product url
if( isset($cat_prod_ids[$_next_pos]) ) {
$_next_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_next_pos] );
} else {
$_next_prod = Mage::getModel('catalog/product')->load( reset($cat_prod_ids) );
}
// load the previous product url
if( isset($cat_prod_ids[$_prev_pos]) ) {
$_prev_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_prev_pos] );
} else {
$_prev_prod = Mage::getModel('catalog/product')->load( end($cat_prod_ids) );
}
?>
<div>
<?php if($_prev_prod != NULL): ?>
<a href="<?php print $_prev_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>"><span><?php echo $this->__('PREVIOUS PRODUCT') ?></span></a>
<?php endif; ?>

<?php if($_next_prod != NULL): ?>
<a href="<?php print $_next_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>"><span><?php echo $this->__('NEXT PRODUCT') ?></span></a>
<?php endif; ?>
</div>

Save the page and refresh your browsewer cache, then you will able to see the Previous and Next products link below the product name on the product description page. Hope this will help to someone.