Oh snap! You are using an old version of browser. Update your browser to get new awesome features. Click for more details.

Magento How to get Previous & Next Product Buttons on Product view Page

If you want to make a good web store, installing Magento is not enough for that. First you should give priority about functional issues of your web store and always keep in mind about Magento’s features. Recently i implement Previous & Next product button on product view page for increase functionality of my web store. To do this i googled and find some good solutions. Here i share about that. You can implement that easily.

I inserted Previous & Next product button after the Product Name in product view page. which is like:

To implement Previous & Next product button you have to edit view.phtml file which is located in:

app/design/frontend/your-instance-name/your-theme/template/catalog/product/view.phtml

If you not found view.phtml there then go:

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

Now open the view.phtml with any code editor. If you want to insert Previous & Next product button after the Product Name as me, then find the following code
 <div class="product-name">
       <h1> 
               <?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?> 
      </h1>
 </div>

Then insert the following code after the above code:

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

$_product = $this->getProduct();

if(!$_product->getCategoryIds())
return; // Don't show Previous and Next if product is not in any category

$cat_ids = $_product->getCategoryIds(); // get all categories where the product is located
$cat = Mage::getModel('catalog/category')->load( $cat_ids[0] ); // load first category, you should enhance this, it works for me

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

$category_products = $cat->getProductCollection()->addAttributeToSort($order, $direction);
$category_products->addAttributeToFilter('status',1); // 1 or 2
$category_products->addAttributeToFilter('visibility',4); // 1.2.3.4

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

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

// get 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) );
}
// get 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> 

hope this information useful ;)

6 comments

  1. Magento Insertion Extension helps eCommerce websites to import products in bulk & integrate feeds into Magento store. With use of product insertion extension, they can easily import group products, Cross-sell, Upsell & Related products, CSV, PDF, and Image Files supported and more.

    ReplyDelete
  2. I carried out AB test on my site: one product page variation had just previous-next links and another one had links with thumbnails of previous and next products. The latter performed much better. So now I'm using pics with prev-next links on my site. This can be made with code but as I'm not good at it, I prefer this extension http://amasty.com/previous-next-navigation.html

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. If you don't want to code this yourself there is a ready solution here: Next and Previous product link for Magento

    ReplyDelete
  5. Thanks for useful sharing.
    There is another way using an extension quickly instead of your code.
    Try Previous/Next Product Page for Magento 2 extension here: http://bsscommerce.com/previous-next-product-page-for-magento-2.html

    ReplyDelete