Monday, November 28

How to get previous-next products url on product view page in magento

Here is the code to get previous/next product url in magento product view page.

Add the following script in catalog/product/view.phtml

<?php
/**
* get the previous/next product's url on product view page
*/
$_curProduct = $this->getProduct();
$_curCategory = $this->helper('catalog/data')->getCategory();
$_prdPosition = $_curCategory->getProductsPosition();
$current_pid = $this->helper('catalog/data')->getProduct()->getId();
 
// build a product array from current products positions
$plist = array();
foreach ($_prdPosition as $pid => $pos) {
 $plist[] = $pid;
}
$_curPosition = array_search($current_pid, $plist);
 
// get the url for previous product
$prevProductId = isset($plist[$_curPosition+1])? $plist[$_curPosition+1] : $current_pid;
$product = Mage::getModel("catalog/product")->load($prevProductId);
$prevProductPosition = $_curPosition;
while (!$product->isVisibleInCatalog()) {
 $prevProductPosition += 1;
 $nextProductId = isset($plist[$prevProductPosition])? $plist[$prevProductPosition] : $current_pid;
 $product = Mage::getModel("catalog/product")->load($nextProductId);
}
$prevProductUrl = $product->getProductUrl();
 
 
// get the url for next product
$nextProductId = isset($plist[$_curPosition-1])? $plist[$_curPosition-1] : $current_pid;
$product = Mage::getModel("catalog/product")->load($nextProductId);
$nextProductPosition = $_curPosition;
while (!$product->isVisibleInCatalog()) {
 $nextProductPosition -= 1;
 $nextProductId = isset($plist[$nextProductPosition])? $plist[$nextProductPosition] : $current_pid;
 $product = Mage::getModel("catalog/product")->load($nextProductId);
}
$nextProductUrl = $product->getProductUrl();
 
 
// get the url for current category
$curCategoryUrl = $_curCategory->getUrl();
 
 
// for html view edit this code block
if($_curProduct->getProductUrl()!= $nextProductUrl){ 
 echo $nextProductUrl
}
 
if(($_curProduct->getProductUrl()!= $nextProductUrl) && ($_curProduct->getProductUrl()!= $prevProductUrl) ) {
 echo "&nbsp;&nbsp; | &nbsp;&nbsp";
}
 
if($_curProduct->getProductUrl()!= $prevProductUrl){
 echo $prevProductUrl;
}
?> 
 
Now we can get the "pre/next" product links in magento product view page...

Hope it Helps... Thanks...

No comments:

Post a Comment