Magento-显示产品所在的类别 [英] Magento - show categories a product is in

查看:63
本文介绍了Magento-显示产品所在的类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码在我的产品页面上显示产品所属的类别.但是我经营着多家具有相同产品的商店,并且它还在显示其他网站的类别.我如何只显示我正在访问的网站的类别?

I use the code below to show the categories that a product is in on my productpage. But i run multi-store with the same products and it is showing also the categories of the other websites. How can i only show the categories of the site that i'm visiting?

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

推荐答案

使用此代码获取当前商店的类别.

User this code to get categories of current store.

$storeId = Mage::app()->getStore()->getStoreId();
     $rootCategoryId = Mage::app()->getStore($storeId)->getRootCategoryId();
     $categoriesCollection = Mage::getModel('catalog/category')
                    ->getCollection()
                    ->setStoreId($storeId)
                    ->addFieldToFilter('is_active', 1)
                    ->addAttributeToFilter('path', array('like' => "1/{$rootCategoryId}/%"))
                    ->addAttributeToSelect('*');
                 foreach($categoriesCollection as $cat)
                    {
                        $id = $cat->getId();                   
                        $name = $cat->getName();             
                    }

这篇关于Magento-显示产品所在的类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆