简体   繁体   English

Magento - 显示产品所在的类别

[英]Magento - show categories a product is in

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

Check loaded category id is exist or not 检查加载的类别ID是否存在

<?php $categories = $_product->getCategoryIds(); ?>
  <?php foreach($categories as $k => $_category_id): ?>
<?php $_category= Mage::getModel('catalog/category')->load($_category_id)?>
     <?php if($_category->getId()):?> 
      <a href="<?php echo $_category->getUrl() ?>">
         <?php echo $_category->getName() ?> | </a>
        <?php endif;?>
   <?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();             
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM