[英]Display ALL categories that a product belongs to in Magento
I am conceptualizing a new Magento site which will have products that are included in several categories. 我正在构思一个新的Magento网站,该网站将包含多个类别的产品。 What I am wondering is if I can display all categories a product is in on the product detail page.
我想知道的是,我是否可以在产品详细信息页面上显示产品所在的所有类别。 I know that it is possible to get the category, but is it possible to display a list of all categories which a product belongs to?
我知道有可能获得该类别,但是是否可以显示产品所属的所有类别的列表?
For example, a shirt may be included in the Shirts category, as well as in Designers and Summer . 例如,衬衫可以包括在衬衫类别中,也可以包括在设计师和夏季中 。 Ideally, I would like to be able to display the following:
理想情况下,我希望能够显示以下内容:
More from:
更多来自:
Men > Shirts
男士>衬衫
Men > Designers > Barnabé Hardy
男装>设计师>BarnabéHardy
Men > Summer
男人>夏天
This will get you the data you are looking for such as the category's name, URL, etc: 这将为您提供所需的数据,例如类别的名称,URL等:
$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('name')
->addAttributeToSelect('url')
->addAttributeToFilter('entity_id', $currentCatIds)
->addIsActiveFilter();
then just iterate over the collection eg 然后只是迭代集合,例如
foreach($categoryCollection as $cat){
echo $cat->getName().' '.$cat->getUrl();
}
Simple. 简单。
$_categories = $_product->getCategoryCollection()
foreach ($_categories as $_category)
//do something with $_category
You can use the following code to display all categories related to the selected product in the product detail page. 您可以使用以下代码在产品详细信息页面中显示与所选产品相关的所有类别。
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
< <a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a>
<?php endforeach; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.