2014-07-02 66 views
1

我怎樣才能得到產品類別的URL鍵如何獲取產品類別URL密鑰?

我可以通過$category->getName()類別名稱,但如果我用這個$category->getURLKey()它不工作,

$categories = $_product->getCategoryCollection() 
    ->addAttributeToSelect('name'); 

foreach($categories as $category) { 
    $productCategoryName = $category->getName(); 
    var_dump($category->getURLKey()); 
} 

返回null

任何想法?

回答

1

我不完全確定你的目標是什麼,但是你不能從$ _product獲取分類信息。隨意根據我的代碼提出問題。但我下面的代碼將抓住活躍的類別的孩子和他們的信息。但是,無論如何,這應該是一個足夠好的基礎。

<!-- Use this section if you're trying to grab children categories and their info --> 

<?php 
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory(); 
$categories = $category->getCollection() 
     ->addAttributeToSelect(array('name', 'thumbnail')) 
     ->addAttributeToFilter('is_active', 1) 
     ->addIdFilter($category->getChildren()) 

?> 
<div class="subcategories"> 
    <p>Select a category to view products:</p> 
    <ul class="clearfix"> 
    <!-- Display Each Subcategory Image and Name Feel Free to remove the img src section to remove the image --> 

    <?php foreach ($categories as $category): ?> 
     <li class="grid12-3"> 
      <a href="<?php echo $category->getUrl() ?>" class="clearfix"> 
       <?php if($thumbFile = $category->getThumbnail()): ?> 
       <img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" /> 
       <?php endif;?> 
       <span><?php echo $category->getName() ?></span></a> 
     </li> 
    <?php endforeach; ?> 
    </ul> 
</div> 
<!-- End --> 

有任何問題隨時問!對不起,我只是從一個項目中偷了一個例子,所以HTML可能不會直接複製。

+0

感謝羅布。我做了這個'$ categories = $ _product-> getCategoryCollection() - > addAttributeToSelect('name') - > addAttributeToSelect('url_key') - > addUrlRewriteToResult();'然後'echo $ category-> getUrlKey()' - 我得到類別的URL鍵:-) – laukok

+1

很高興聽到!別客氣。 – Rob

1
foreach($categories as $category) { 
// for category URL key 
var_dump($category->getUrlPath()); 

// for category URL 
var_dump($category->getUrl()); 
}