2016-09-06 168 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();    
        } 
相关问题