在 Magento 中以编程方式添加面包屑路径? [英] Programmatically add breadcrumbs paths in Magento?
问题描述
在 Magento 中,当用户直接访问产品页面时,例如从 Google 访问,面包屑将只会是主页"->产品名称".
In Magento, when the user directly accesses the product page, such as from Google, the breadcrumbs would only be "Home" -> "Product Name".
例如,在 此页面,我想在面包屑中添加婚礼服装"和婚礼礼服"类别.我想出了一个想法,而不是硬编辑面包屑.phtml,但有什么方法可以以编程方式在模板/目录/产品/view.phtml 中添加面包屑项目?
For example, on this page, I want to add the categories "Wedding Apparel" and "Wedding Dresses" in the breadcrumbs. I came up with an idea other than hard editing breadcrumbs.phtml but is there any way I can programmatically add a breadcrumbs item in template/catalog/product/view.phtml ?
我可以获取当前产品的类别(标题和链接),然后使用一些函数/方法以动态和编程的方式将它们添加到面包屑中.这可能吗?
I can get the categories (title and link) of the current product and then use some function / method to add them in the breadcrumbs dynamically and programmatically. Is this possible?
推荐答案
这是强制 Magento 显示完整面包屑的代码,包括通过循环遍历当前产品的每个类别的类别:
Here's the code that forces Magento to display the complete breadcrumb, including categories by looping trough each category for the current product:
©丹尼文斯
<?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
);
}
}
?>
这篇关于在 Magento 中以编程方式添加面包屑路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!