Magento - show categories a product is in

不打扰是莪最后的温柔 提交于 2019-12-02 06:39:51

问题


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

回答1:


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



回答2:


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();             
                    }


来源:https://stackoverflow.com/questions/39340166/magento-show-categories-a-product-is-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!