magento产品详细页添加下一件、上一件产品链接

1,911 人次阅读
没有评论

共计 2021 个字符,预计需要花费 6 分钟才能阅读完成。

此代码放在你的magento系统的view.phtml文件中即可。
代码如下:

[php]
<!–?php $_helper = $this—>helper(‘catalog/output’);//如果view.phtml文件中已经存在,可不加
$_product = $this->getProduct();//如果view.phtml文件中已经存在,可不加
$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();
}
?></pre>
<div class="”prevNextcategoriesNavigation”">
<div class="”for-next”"><!–?php if ($url != $prev_url):?–>
<a class="”next-product”" href="/templates/default/&rdquo">”>
<!–?php echo $this—>__(‘Prev’) ?>
</a>
<!–?php endif; ?–></div>
<div class="”for-prev”"><!–?php if ($url != $next_url):?–>
<a class="”prev-product”" href="/templates/default/&rdquo">”>
<!–?php echo $this—>__(‘next’) ?>
</a>
<!–?php endif; ?–></div>
</div>
<pre>
[/php]

我们的一贯宗旨是让专业的人做专业的事情,请不要在不确定你能自己的时候,自己去做。让给专业的人吧。

正文完
 0
评论(没有评论)