Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Category Thumbnail

I am trying to make use of the Magento Category thumbnail, but it is not working. I've followed many tutorials online (eg http://www.douglasradburn.co.uk/getting-category-thumbnail-images-with-magento/), and all of them make mention of the function :

$_category->getThumbnail()

which is supposed to be in the Category model. I'm running Magento 1.6 and I can't find this function anywhere. I've also downloaded 1.5 and 1.7, looked in there and it is nowhere to be found. When I run the code it gives me no errors however, just nothing is output.

Here is my full code:

 <ul id="nav">
 <?php foreach ($this->getStoreCategories() as $_category): ?>
     <?php echo $_category->getThumbnail(); ?>  
     <?php echo $this->drawItem($_category) ?>
 <?php endforeach ?>
 </ul>

(I am trying to use the thumbnail as a menu item where it is present)


Got it working. The secret is you need to re-query for the FULL category data using this code:

Mage::getModel('catalog/category')->load($_category->getId())->getThumbnail()

I followed this tutorial somewhat:

http://www.h-o.nl/blog/using_category_images_in_your_magento_navigation/

for having category thumbnails in your menu.

thanks T

like image 578
Totomobile Avatar asked May 17 '12 22:05

Totomobile


4 Answers

For what it's worth, your solution works but is quite inefficient.

Using:

Mage::getModel('catalog/category')->load($_category->getId())->getThumbnail()

will add a few hundredths, maybe even tenths of a second per category to your page's load time.

The reason for this is you've gone to the trouble of getting a model collection and getting the item within it, and then you'll be adding new database calls that fetch the full data for each category. You need to simply ensure you collect the full category data in the first place.

The reason what you had before wasn't working is because the category collection wasn't told what attributes it needs to select. It was in effect just returning flat data from the catalog_category_entity table, not joined with any attribute tables.

What you need to do is probably more along these lines:

<ul id="nav">
<?php foreach ($this->getStoreCategories()->addAttributeToSelect("*") as $_category): ?>
    <?php echo $_category->getThumbnail(); ?>  
    <?php echo $this->drawItem($_category) ?>
<?php endforeach ?>
</ul>

In fact, ideally you want to override the ->getStoreCategories() function to add the wildcard filter.

I recommend opening app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php and learning what sort of very cool collection functions have been written. Mastering EAV Collections is like a rite of passage for Magento developers. Once you do this you'll be unstoppable!

Hope this helps.

like image 93
Magento Guy Avatar answered Nov 13 '22 19:11

Magento Guy


There is no need to change app/code/local/Mage/Catalog/Model/Category.php

It can be done easily through these line of code...try this...Its Works

$child= Mage::getSingleton('catalog/layer')->getCurrentCategory()->getId();

$imageSrc = Mage::getModel('catalog/category')->load($child)->getThumbnail();

$ThumbnailUrl = Mage::getBaseUrl('media').'catalog/category/'.$imageSrc;

echo "<img src='{$ThumbnailUrl}' />";
like image 45
IamManish Avatar answered Nov 13 '22 20:11

IamManish


this worked for me:

<img src="http://etienneaigner.com/shop/media/catalog/category/
     <?php echo Mage::getModel('catalog/category')->load($_category->getId())->getThumbnail(); ?>"

     height="338px" width="338px"
     alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
like image 2
frank Avatar answered Nov 13 '22 19:11

frank


Just came across this answer. However, in later magento editions 1.7+ there is no need to add overheads to code, you can add more standard (and custom) eav attributes to the Category collection via config.xml. If you check the Mage/Catalog/etc/config.xml you will notice under the node that there are nodes:

   <category>
        <collection>
            <attributes>
                <name/>
                <url_key/>
                <is_active/>
            </attributes>
        </collection>
    </category>

So you can create a module of your own and add more eav attributes:

    <category>
        <collection>
            <attributes>
                <thumbnail/>
                <image/>
            </attributes>
        </collection>
    </category>

And they will be added to your category collection.

like image 1
Robert Avatar answered Nov 13 '22 21:11

Robert