• Home
  • Magento
  • Contact
KEEP IN TOUCH

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

Facebook Twitter Google + Share Reddit Digg Stumble Delicious More

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 (line no:49 in version-1.5):

  1. <div class="product-name">
  2.     <h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
  3. </div>
<div class="product-name">
	<h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
</div>

Then insert the following code after the above code:

  1. <?php // Previous and Next product links in product page
  2.  
  3. $_product = $this->getProduct();
  4.  
  5. if(!$_product->getCategoryIds())
  6. return; // Don't show Previous and Next if product is not in any category
  7.  
  8. $cat_ids = $_product->getCategoryIds(); // get all categories where the product is located
  9. $cat = Mage::getModel('catalog/category')->load( $cat_ids[0] ); // load first category, you should enhance this, it works for me
  10.  
  11. $order = Mage::getStoreConfig('catalog/frontend/default_sort_by');
  12. $direction = 'asc'; // asc or desc
  13.  
  14. $category_products = $cat->getProductCollection()->addAttributeToSort($order, $direction);
  15. $category_products->addAttributeToFilter('status',1); // 1 or 2
  16. $category_products->addAttributeToFilter('visibility',4); // 1.2.3.4
  17.  
  18. $cat_prod_ids = $category_products->getAllIds(); // get all products from the category
  19. $_product_id = $_product->getId();
  20.  
  21. $_pos = array_search($_product_id, $cat_prod_ids); // get position of current product
  22. $_next_pos = $_pos+1;
  23. $_prev_pos = $_pos-1;
  24.  
  25. // get the next product url
  26. if( isset($cat_prod_ids[$_next_pos]) ) {
  27. $_next_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_next_pos] );
  28. } else {
  29. $_next_prod = Mage::getModel('catalog/product')->load( reset($cat_prod_ids) );
  30. }
  31. // get the previous product url
  32. if( isset($cat_prod_ids[$_prev_pos]) ) {
  33. $_prev_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_prev_pos] );
  34. } else {
  35. $_prev_prod = Mage::getModel('catalog/product')->load( end($cat_prod_ids) );
  36. }
  37. ?>
  38.  
  39. <div>
  40. <?php if($_prev_prod != NULL): ?>
  41. <a href="<?php print $_prev_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>"><span><?php echo $this->__('PREVIOUS PRODUCT') ?></span></a>
  42. <?php endif; ?>
  43.  
  44. <?php if($_next_prod != NULL): ?>
  45. <a href="<?php print $_next_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>"><span><?php echo $this->__('NEXT PRODUCT') ?></span></a>
  46. <?php endif; ?>
  47. </div>
<?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>

Now save the page & Refresh the product view page of your web store.

Note: You can also use the above code anywhere of the page where you want to show those Buttons.

Related Topics

  • Change default sort order direction (ASC to DESC) for product listing in Magento
  • How to include "Add to Compare" link on Related Products in Magento
  • How to remove auto added break (<br />) tag from product description of magento
  • Hide Attributes from frontend which values are not given from backend in Magento
  • How to show products on Magento Homepage from specific Category
Posted in Magento, Tips and Tricks- Tagged Previous-Next buttons in Magento
6 CommentsWritten by Admin

6 Comments

  1. Jonas
    September 18, 2012 at 4:05 PM | Permalink

    Nice tutorial about Next-Previous product button, but I’ll try to implement it in a local.xml file, then it’s update proof.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


eight − = 7

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" extra="">

E-mail Subscription

Enter your email address:

Recent Posts

BPL Fixture 2013 | Download Bangladesh Premier League Time-Table

BPL Fixture 2013 | Download Bangladesh Premier League Time-Table

Bangladesh Premier League BPL 2013 All Teams and Players List

Bangladesh Premier League BPL 2013 All Teams and Players List

National University Result | Get Bangladesh NU Result

National University Result | Get Bangladesh NU Result

JSC (Junior School Certificate) And JDC Exam Result Bangladesh

JSC (Junior School Certificate) And JDC Exam Result Bangladesh

Primary Somapony Result | PSC (Primary School Certificate) BD

Primary Somapony Result | PSC (Primary School Certificate) BD

Download pdf 33rd BCS written exam routine and Seat Plan 2012

Download pdf 33rd BCS written exam routine and Seat Plan 2012

Degree 1st Year Final Routine of National University (NU) BD

Degree 1st Year Final Routine of National University (NU) BD

JSC (Junior School Certificate) Exam Routine 2012 Download PDF

JSC (Junior School Certificate) Exam Routine 2012 Download PDF

National University (NU) Honours Final (4th) Year Routine Download

National University (NU) Honours Final (4th) Year Routine Download

NU (National University) Honours 1st Year Routine PDF Download

NU (National University) Honours 1st Year Routine PDF Download

How to get oDesk money directly on Bank account from Bangladesh

How to get oDesk money directly on Bank account from Bangladesh

Dhaka University (DU) Admission Test Routine for 2012-2013

Dhaka University (DU) Admission Test Routine for 2012-2013

Recent Comments

  • chayan on National University Result | Get Bangladesh NU Result
  • Stan on Creating and Showing a static block in Magento Frontend
  • Stan on Creating and Showing a static block in Magento Frontend
  • Dani on How to get custom attributes value in Magento Frontend
  • Kiran on How to show products on Magento Homepage from specific Category
  • Bryan on How to Remove Callout section from left / right side of Magento
  • Sawon on Installing Magento on Localhost using WAMP Server
  • Keyur Shah on Installing Magento on Localhost using WAMP Server

Categories

  • Bangladesh Xpress
  • Magento
  • Sports
  • Tips and Tricks

EvoLve theme by Theme4Press  •  Powered by WordPress XpressBangla

Copyright © 2012 XpressBangla. All rights reserved.
You might also likeclose
  • Sunday, 06/01/2013 12:19 AM
    BPL Fixture 2013 | Download Bangladesh Premier League Time-Table
  • Friday, 28/12/2012 3:25 PM
    Bangladesh Premier League BPL 2013 All Teams and Players List