Magento News

 

Magento Module: Hide Empty Categories

There are a number of ways that you can hide empty categories: You can disable them manually, you can modify the template output to check the product count and if zero don’t display it, etc. Another way is to write a module and intercept the catalog collection output before it’s sent to the templates.

I could easily package this up and put it up on Magento Connect, but I’m more into trying to help people understand Magento modules, so below is a walkthrough of how to create a module that will provide this functionality.

Note: This currently does NOT work with Flat Categories enabled. This has only been tested on 1.5.x.

Step 1

Create the file ‘/app/etc/modules/Prattski_HideEmptyCategories.xml’. This file tells Magento that your module exists, if it’s enabled or not, and where to find it.

<?xml version="1.0"?>
<!--
/**
 * Hide Empty Categories
 *
 * @category    Prattski
 * @package     Prattski_HideEmptyCategories
 * @copyright   Copyright (c) 2011 Prattski (http://prattski.com/)
 * @author      Josh Pratt (Prattski)
 */
-->
<config>
    <modules>
        <Prattski_HideEmptyCategories>
            <active>true</active>
            <codePool>local</codePool>
        </Prattski_HideEmptyCategories>
    </modules>
</config>

Step 2

Now we create our module directory. Create ‘/app/code/local/Prattski/HideEmptyCategories/’. This is where our module will reside. Inside that directory, create this file: ‘etc/config.xml’. This file does a number of things: It tells Magento which version your module is, defines your model namespace and directory, and creates and event observer to observe every time a category collection is loaded.

<?xml version="1.0"?>
<!--
/**
 * Hide Empty Categories
 *
 * @category    Prattski
 * @package     Prattski_HideEmptyCategories
 * @copyright   Copyright (c) 2011 Prattski (http://prattski.com/)
 * @author      Josh Pratt (Prattski)
 */
-->
<config>
    <modules>
        <Prattski_HideEmptyCategories>
            <version>1.0.0</version>
        </Prattski_HideEmptyCategories>
    </modules>
    <global>
        <models>
            <hideemptycategories>
                <class>Prattski_HideEmptyCategories_Model</class>
            </hideemptycategories>
        </models>
    </global>
    <frontend>
        <events>
            <catalog_category_collection_load_after>
                <observers>
                    <hideemptycategories>
                        <type>singleton</type>
                        <class>hideemptycategories/observer</class>
                        <method>catalogCategoryCollectionLoadAfter</method>
                    </hideemptycategories>
                </observers>
            </catalog_category_collection_load_after>
        </events>
    </frontend>
</config>

Step 3

Let’s create our observer model. As you can see in the file above, we are going to have the ‘catalogCategoryCollectionLoadAfter’ method run in this file when it observes the ‘catalog_category_collection_load_after’ event. Create this file: Model/Observer.php

<?php
/**
 * Hide Empty Categories
 *
 * @category    Prattski
 * @package     Prattski_HideEmptyCategories
 * @copyright   Copyright (c) 2011 Prattski (http://prattski.com/)
 * @author      Josh Pratt (Prattski)
 */
 
/**
 * Event Observer
 *
 * @category    Prattski
 * @package     Prattski_HideEmptyCategories
 */
class Prattski_HideEmptyCategories_Model_Observer extends Mage_Core_Model_Abstract
{
    /**
     * Remove hidden caegories from the collection
     *
     * @param Varien_Event_Observer $observer
     */
    public function catalogCategoryCollectionLoadAfter($observer)
    {
        if ($this->_isApiRequest()) return;
        $collection = $observer->getEvent()->getCategoryCollection();
        $this->_removeHiddenCollectionItems($collection);
    }
 
    /**
     * Remove hidden items from a product or category collection
     *
     * @param Mage_Eav_Model_Entity_Collection_Abstract|Mage_Core_Model_Mysql4_Collection_Abstract $collection
     */
    public function _removeHiddenCollectionItems($collection)
    {
        // Loop through each category or product
        foreach ($collection as $key => $item)
        {
            // If it is a category
            if ($item->getEntityTypeId() == 3) {
 
                if ($item->getProductCount() < 1) {
                    $collection->removeItemByKey($key);
                }
            }
        }
    }
 
    /**
     * Return true if the reqest is made via the api
     *
     * @return boolean
     */
    protected function _isApiRequest()
    {
        return Mage::app()->getRequest()->getModuleName() === 'api';
    }
}

Step 4

To recap all the files we have made:

  • /app/etc/modules/Prattski_HideEmptyCategories.xml
  • /app/code/local/Prattski/HideEmptyCategories/etc/config.xml
  • /app/code/local/Prattski/HideEmptyCategories/Model/Observer.php

Test it out! You’ll be able to see if Magento recognizes your module by going to System > Configuration > Advanced. If you see the module in the list, Magento knows it’s there. You’ll have to have some categories in your system, some with products in them, some without, to see if it’s working properly on the frontend.

I hope you enjoyed learning how to build this module. If you find any issues or bugs, please let me know.


Magento Module: Hide Empty Categories

Possibly Related Posts:


 

Leave a Reply