Magento - show categories a product is in

前端 未结 2 1096
鱼传尺愫
鱼传尺愫 2021-01-24 10:59

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 oth

相关标签:
2条回答
  • 2021-01-24 11:13

    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();             
                        }
    
    0 讨论(0)
  • 2021-01-24 11:24

    Check loaded category id is exist or not

    <?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; ?>
    
    0 讨论(0)
提交回复
热议问题