簡體   English   中英

Magento - 顯示產品所在的類別

[英]Magento - show categories a product is in

我使用下面的代碼顯示產品在我的產品頁面上的類別。 但是,我使用相同的產品運行多店,並且還顯示其他網站的類別。 我怎樣才能顯示我正在訪問的網站的類別?

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

檢查加載的類別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; ?>

使用此代碼獲取當前商店的類別。

$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