1

I have the following static block to display my sub-categories in the categories page:

<div class="category-products2">
<ul class="products-grid subGrid">
<?php
$_categories=$this->getCurrentChildCategories();
if($_categories->count()):
    $categorycount = 0;
foreach ($_categories as $_category):
    if($_category->getIsActive()):
        $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
    $layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
if ($categorycount == 0){
    $class = "first";
}
elseif ($categorycount == 3){
    $class = "last";
}
else{
    $class = "";
}
?>
<li class="item <?php echo $class?> dropshadowclass">
<a class="product-image" href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><img src="<?php echo $_category->getImageUrl() ?>" width="100%" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" /></a>

<h2 class="subImageCat"><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a></h2>
</li>
<?php
endif;
if($categorycount == 3){
    $categorycount = 0;
    echo "</ul>\n\n<ul class=\"products-grid subGrid\">";
}
else{
    $categorycount++;
}
endforeach;
endif;
?>
</ul>
</div>

Also, have amended the method getCurrentChildCategories() to:

 $layer = Mage::getSingleton('catalog/layer');
    $category   = $layer->getCurrentCategory();
    /* @var $category Mage_Catalog_Model_Category */
    $collection = Mage::getModel('catalog/category')->getCollection();
    /* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
    $collection->addAttributeToSelect('url_key')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('is_anchor')
        ->addAttributeToSelect('image') 
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($category->getChildren())
        ->setOrder('position', 'ASC')
        ->joinUrlRewrite()
        ->load();

    $productCollection = Mage::getResourceModel('catalog/product_collection');
    $layer->prepareProductCollection($productCollection);
    $productCollection->addCountToCategories($collection);
    return $collection;

However, my issue is that the cache, in particular the Blocks HTML output cache is caching the sub-categories of the first category page I visit and then displaying them for any other subsequent category pages I visit.

Would anyone be able to kindly myself identify what is causing this issue ?

Your Answer

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

Browse other questions tagged or ask your own question.