Magento产品详细页添加返回分类链接,下一件,上一件产品链接

2,171 人次阅读
没有评论

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

部分代码来源:http://www.magentocommerce.com/boards/viewthread/9946/P30/

将此代码放在你的Magento系统\app\design\frontend\default\Your_Template\template\catalog\product\view.phtml文件中

[php]

<?php
$_helper = $this->helper(‘catalog/output’);
$_product = $this->getProduct();
?>

[/php]

下一行即可。
代码如下:

[php]

<!–for next & prev link–>
<?php // prev- / next-link…
    $_helper = $this->helper(‘catalog/output’);
    $_product = $this->getProduct();
    $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();
    }
?>
<div>
    <div>
        <?php if ($url != $prev_url):?>
            <a href="<?php echo $prev_url; ?>">
                <?php echo $this->__(‘<- Prev’) ?>
            </a>
        <?php endif; ?>
    </div>
    <div>
        <ul>
            <li> <?php echo $this->__(‘Back to Categories:’) ?></li>
            <?php $categories = $_product->getCategoryIds(); ?>
            <?php foreach($categories as $k => $_category_id): ?>
            <?php $_category = Mage::getModel(‘catalog/category’)->load($_category_id) ?>
            <li><a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName()

?></a></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div>
        <?php if ($url != $next_url):?>
            <a href="<?php echo $next_url; ?>">
                <?php echo $this->__(‘next ->’) ?>
            </a>
        <?php endif; ?>
    </div>
</div>
<!–for next & prev link–>

[/php]

至于按钮样式,请自行到CSS文件中添加修改。

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