0

My store running on Magento 1.942.

I want to display subcategories on a category page, and find this guide https://www.magemonkeys.com/how-to-display-sub-categories-on-a-category-page-in-magento/.

I create a Static Blocks and subcategory_listing.phtml according to the guide, everything works fine.

but when I enable cache in "Cache Storage Management", All parent categories on my store display the same subcategories (For example, after refreshing the Magento cache, if I click on the A parent category and then click on the other parent category, the subcategories under the A parent category are displayed.).

I refreshed the Magento cache, the browser cache, but nothing changed.

here are the codes of subcategory_listing.phtml

<?php
$layer = Mage::getSingleton('catalog/layer');
$_category  = $layer->getCurrentCategory();
$_categories = $_category->getCollection()->addAttributeToSelect(array('url_key','name','image','all_children','is_anchor','description'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($_category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
?>
<div class="listing-type-list catalog-listing">
<ul id="subcats" class="clear">
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<?php Mage::log($_category->debug(), null, 'mylogfile.log'); ?>
<li>
<div class="category-box clearfix">
<a class="now-from-container" href="<?php echo $_category->getURL() ?>"></a>
<div class="category-image-box">
<a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>">
<img src="<?php echo $this->htmlEscape($_category->getImageUrl()) ?>" width="135" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" />
</a>
</div>
<div class="category-name">
<h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a></h2>
</div>
</div>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ul>
</div>
<div style="clear:both;"> </div>

  • Does anyone reply to me? – Clark Oct 8 at 23:41
0

I am looking for a solution as well on 1.9.4.2
Looks like an issue similar to what happened in 1.9.2 documented in the link below.
Blocks HTML Cache & Sub Category Display

I've tried these two modules and neither did the trick.
https://github.com/kirchbergerknorr/Milople_Cms
https://github.com/progammer-rkt/Rkt_SbCache

Edit: Here is what I ended up doing:

Previously I was trying to add the block as a static block calling the phtml file. On this link I got the idea to turn off the cache on the block via xml. https://www.creare.co.uk/blog/magento/magento-subcategories-category-pages

I ended up using the following method to get subcategories to display correctly and not duplicate for each category: <action method="setCacheLifetime"><s>null</s></action>
Which I believe prevents the subcategory block from being cached and duplicating.

My subcategory block was located at ...catalog/navigation/sub_category_listing.phtml

I added my sub categories block to my catalog.xml with:
<block type="catalog/navigation" name="sub_category_listing" before="category.products" template="catalog/navigation/sub_category_listing.phtml"> <action method="setCacheLifetime"><s>null</s></action> </block>

And positioned the block with the following in the view.phtml file:

<?php echo $this->getLayout()->createBlock('catalog/navigation')->setTemplate('catalog/navigation/sub_category_listing.phtml')->toHtml(); ?>

I am not going to show the code I used for the subcategory block since I don't know where I got it and there are probably much better examples now (was from an old site I am upgrading). Hope this helps!

  • I use your method, and it is working fine on my website, thank you very much! – Clark Oct 10 at 1:37
  • Glad I could help! Thanks – danbacci Oct 10 at 5:44
  • Clark, can you accept my answer if it worked? Thanks! – danbacci Oct 13 at 21:44

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.