Search code examples
phpmagentomagento-1.9

Get category name from Magento


I'm using Luxury theme in Magento. I'm trying to display current category name in the catalog/category/view.phtml file.

What I have done so far:

<div class="custom">
<?php if($crumbs && is_array($crumbs)): ?>
    <div class="container">
        <div class="col-md-12">
            <ul>
                <?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
                    <li class="<?php echo $_crumbName ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb" <?php endif ?>>
                    <?php if($_crumbInfo['link']): ?>
                        <a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="url" <?php endif ?>><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></a>
                    <?php else: ?>
                        <strong><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></strong>
                    <?php endif; ?>
                    <?php if(!$_crumbInfo['last']): ?>
                        <span>| </span>
                    <?php endif; ?>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </div>
<?php endif ?>

</div>

I have taken this code from page/html/breadcrumbs.phtml.

I am totally new to Magento/PHP. It doesn't show any error but it's not displaying the name of the category, while it's visible in breadcrumbs header. What I am doing wrong here?


Solution

  • On our site (magento 1.9) we wanted to show the first parent category of the current product on our product pages and provide a link to it. I achieved this as follows - you should be able to reverse engineer my code to your own ends. At first I did it by adding the following code directly to the catalog/product/view.phtml but have since migrated it into a custom helper in my own module.

    Here's the code, see if it works for you.

    //get an array of the IDs of every category to which the product belongs.
    $categoryIds = $_product->getCategoryIds();
    
    //set CatID to the second element of the array since the first element 
    //is the base category of which all others are children.
    $_catID = $categoryIds[1];
    
    //load the correct model
    $_category = Mage::getModel('catalog/category')->load($_catID);
    
    //get the level of the current category
    $level = $_category->getLevel();
    
    //This if statement prevents the function from trying to run on products
    //which are not assigned to any category.
    if($_category->getName()){
    
        // we want the second level category, since the first is the base category.
        // ie if we call the default category 'base' and the product is in category 
        //base->foo->bar->baz we want to return the link to foo.
        // while current category is deeper than 2 we ask it for it's parent  
        //category until we reach the one we want
    
        while ($level > 2){
                $parent = $_category->getParentId();
                $_category =Mage::getModel('catalog/category')->load($parent);
                $level = $_category->getLevel();
            }
    
            //Now we can build the string and echo it out to the page
            $caturl = $_category->getUrl_key();
            $_linkstring =  'http://www.yourwebsite.com/' . $caturl . '.html';
            echo 'in category:';
            echo '<a href="' . $_linkstring . '" title="'. $_category->getName() .'">';
            echo ' '. $_category->getName();
            echo '</a>';
    }