DreamWave
DreamWave

Reputation: 1940

How to get only categories with images in Magento?

I have a huge list of subcategories in a category. When opening that category, I need to display only the subcategories that have images set. Is there a way to filter the category collection to display only categories that:

Upvotes: 0

Views: 922

Answers (3)

Mihai Stancu
Mihai Stancu

Reputation: 16107

Magento collections & queries:

You can use a Magento category_collection to build a query. Magento collection queries extend Zend queries so you can use a lot of things a Zend query would allow you to use (or you can get the zend query and alter it directly).

One nice addition that Magento collection queries bring is the fact that you can filter by EAV attributes (it adds all the necessary table joins to fetch the attributes).

Another nice addition of Magento collections is the fact that it fetches information from either the current store if available or from the default store.

Answer:

$categories = Mage::getResourceModel('catalog/category_collection')
    ->addFieldToFilter('image', array('notnull' => true))
    ->addFieldToFilter('parent_id', $categoryId)
    ->setStoreId(2);

In our case both image and parent_id are simply fields in the catalog_category_entity table, they are not EAV attributes so we can use addFieldToFilter. If they were EAV attributes we should use addAttributeToFilter.

Flat tables:

There is also an issue regarding how these queries work when flat tables are enabled I don't remember what the catch was exactly. For example I just tested this query using addAttributeToFilter('image' even though the image is a Field not an attribute, and the query worked for me because the shop I tested it in is using flat tables (in flat tables attributes are converted to fields).

Impact on server:

There are a few performance elements to monitor:

  1. communication between PHP and SQL (number of individual queries)
  2. complexity / efficiency / speed of individual queries
  3. PHP data processing
  4. PHP memory usage
  5. HTML/CSS/JS/Images files download time
  6. HTML/CSS/JS/Images rendering time

And there are a few performance enhancements built into Magento:

  1. Flat tables and other indexes (reduces the overhead of EAV SQL queries) reducing the complexity of many SQL queries (reduces the impact of issue nr.2)
  2. Caching -- either file cache, memcached or other -- reduces the number of individual SQL queries as well as a lot of PHP data processing (reduces the impact of issues nr.1 and 3.)
  3. Merging and minifying CSS and JavaScript files (reduces the impact of issues nr.5 and 6)
  4. Resizing the images from a normal full size to a thumbnail size -- this should reduce having to download big images and having to render them into small areas (also reduces the impact of issues nr.5 and 6)

Upvotes: 3

Slimshadddyyy
Slimshadddyyy

Reputation: 4073

To display the category image in place of a category name.

<?php
    //gets all sub categories of parent category 'Brands'
    $cats = Mage::getModel('catalog/category')->load(6)->getChildren();
    $catIds = explode(',',$cats);

    $categories = array();
    foreach($catIds as $catId) {
        $category = Mage::getModel('catalog/category')->load($catId); 
        $categories[$category->getName()] = array(
            'url' => $category->getUrl(),
            'img' => $category->getImageUrl()
        );
    }

    ksort($categories, SORT_STRING);
?>
    <ul>
        <?php foreach($categories as $name => $data): ?>
            <li>
                <a href="<?php echo $data['url']; ?>" title="<?php echo $name; ?>">
                    <img class="cat-image" src="<?php echo $data['img']; ?>" />
                </a>
            </li>   
        <?php endforeach; ?>
    </ul>

Upvotes: 0

Kubol
Kubol

Reputation: 136

Look this Click - Magento page

Upvotes: -1

Related Questions