2016-09-06 213 views
1

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

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

試試這個。 http://stackoverflow.com/questions/15505221/how-do-i-get-the-category-ids-that-a-product-is-in-with-respect-to-the-store-tha –

回答

0

檢查裝入的類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; ?> 
0

用戶此代碼來獲取當前店鋪的類別。

$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();    
        } 
相關問題