1

My xml site map is showing categories which contain no products (although some child categories may). Is there a simple way of removing these from the xml or do I need to count products as magento loops through the category tree?

2

To do it, we need rewrite two classes:

1. Mage_Sitemap_Model_Resource_Catalog_Abstract

The _addFilter method will be responsible to filter our category collection. But _addFilter has just to condition filter: equal and in:

protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
{
    [...]

    switch ($type) {
        case '=':
            $conditionRule = '=?';
            break;
        case 'in':
            $conditionRule = ' IN(?)';
            break;
        default:
            return false;
            break;
    }

    [...]

So, we need do a new conditionRule to get category with a count greater than zero:

protected function _addFilter($storeId, $attributeCode, $value, $type = '=')
{
    [...]

    switch ($type) {
        case '=':
            $conditionRule = '=?';
            break;
        case 'in':
            $conditionRule = ' IN(?)';
            break;
        case '>': // Our new case here
            $conditionRule = '>?';
            break;
        default:
            return false;
            break;
    }

    [...]

2. Mage_Sitemap_Model_Resource_Catalog_Category

Now we just need filter our collection in getCollection:

public function getCollection($storeId)
{
    [...]

    $this->_addFilter($storeId, 'is_active', 1);

    /* HERE YOU WILL PUT A NEW FILTER */
    $this->_addFilter($storeId, 'children_count', 0, '>');

    return $this->_loadEntities();
}

Note: Don't hack the core, build a extension to rewrite it.

Note2: I tested it. It works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.