在 Magento 中显示产品所属的所有类别

发布于 2024-10-03 20:44:23 字数 355 浏览 4 评论 0原文

我正在构思一个新的 Magento 网站,该网站将包含多个类别的产品。我想知道是否可以在产品详细信息页面上显示产品所属的所有类别。我知道可以获取类别,但是是否可以显示产品所属的所有类别列表?

例如,衬衫可能包含在衬衫类别中,以及设计师夏季类别中。理想情况下,我希望能够显示以下内容:

更多来自:

   男士>衬衫

   男士>设计师>巴纳贝·哈迪

   男士>夏天

I am conceptualizing a new Magento site which will have products that are included in several categories. 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

   Men > Summer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒几许 2024-10-10 20:44:23

这将为您提供您正在寻找的数据,例如类别的名称、URL 等:

$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('url')
                     ->addAttributeToFilter('entity_id', $currentCatIds)
                     ->addIsActiveFilter();

然后只需迭代集合即可,例如

foreach($categoryCollection as $cat){
  echo $cat->getName().' '.$cat->getUrl();
}

This will get you the data you are looking for such as the category's name, URL, etc:

$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
                     ->addAttributeToSelect('name')
                     ->addAttributeToSelect('url')
                     ->addAttributeToFilter('entity_id', $currentCatIds)
                     ->addIsActiveFilter();

then just iterate over the collection e.g.

foreach($categoryCollection as $cat){
  echo $cat->getName().' '.$cat->getUrl();
}
各自安好 2024-10-10 20:44:23

简单的。

$_categories = $_product->getCategoryCollection()
foreach ($_categories as $_category)
    //do something with $_category

Simple.

$_categories = $_product->getCategoryCollection()
foreach ($_categories as $_category)
    //do something with $_category
混吃等死 2024-10-10 20:44:23

您可以使用以下代码在商品详情页中显示与所选商品相关的所有类别。

<?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; ?>

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; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文