以编程方式在Magento中添加面包屑路径?

时间:2012-09-25 05:23:38

标签: magento

在Magento中,当用户直接访问产品页面时,例如来自Google,面包屑只会是“Home” - > “产品名称”。

How can I add categories in there even when users access the page directly from Google?

例如,在this page上,我想在面包屑中添加“Wedding Apparel”和“Wedding Dresses”类别。我提出了一个想法,而不是硬编辑breadcrumbs.phtml,但是有什么方法可以在编辑模板/ catalog / product / view.phtml中以编程方式添加breadcrumbs项目吗?

我可以获取当前产品的类别(标题和链接),然后使用一些函数/方法以动态和编程方式将它们添加到面包屑中。这可能吗?

1 个答案:

答案 0 :(得分:4)

这是迫使Magento显示完整面包屑的代码,包括通过循环显示当前产品的每个类别的类别:

©Danny Vince

<?php
if ($product = Mage::registry('current_product')) {
    $categories = $product->getCategoryCollection()->load();

    if($categories) {
        foreach ($categories as $category)
        {
            if($category) {
            $category = Mage::getModel('catalog/category')->load($category->getId());
            break;
            }
        }
    }
    $lastCrumbName = $product->getName();
    $lastCategoryAdjust = 0;
}
else {
    if($category = Mage::registry('current_category')) {
    $lastCrumbName = $category->getName();
    }
    $lastCategoryAdjust = 1;
}

if($category) {
    if($path = $category->getPath()) {
        $path = explode('/', $path);
        $crumbs = array('home' => array('label' => 'Home',
        'title' => 'Home',
        'link' => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
        'first' => true,
        'last' => false
        ));
        for($i = 2; $i < count($path) - $lastCategoryAdjust; $i++) {
            $cur_category = Mage::getModel('catalog/category')->load($path[$i]);
            if($cur_category && $cur_category->getIsActive()) {
                $crumbs['category' . $path[$i]] = array('label' => $cur_category->getName(),
                'title' => $cur_category->getName(),
                'link' => $cur_category->getUrl(),
                'first' => false,
                'last' => false
                );
            }
        }
        $crumbs['current'] = array('label' => $lastCrumbName,
        'title' => '',
        'link' => '',
        'first' => false,
        'last' => true
        );
    }
}
?>