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>
Hi @tonyzyc,
Maybe you can check this free module (or even you can try it): https://github.com/amarroni/Magento-Module-AM-SubCatMode
As you can see, allows you to show subcategories.
If you don't want to use it maybe you can check how the blocks are being created.
I completely follow the above tutorial, this is the static block I added.
Step:
1. add Static Blocks
2. Create a new file “subcategory_listing.phtml” to
app\design\frontend\smartwave\porto\template/catalog/navigation/
3. set the category, at Display mode tab select Static Block only, and click Save Category
What went wrong with my steps?
I will try this free module late, thank you
Hello @tonyzyc
You can create a separate block for subcategories and To add caching functionality to your block getCacheKeyInfo(). Please follow following script.
<?php
class Vendor_CategoryNavigation_Block_Subcategorynav extends Mage_Catalog_Block_Navigation
{
protected $_categories = null;
protected $_categoryImageUrl = null;
protected $_currentEntityKey;
protected $_currentCategoryKey;
const CACHE_TAG = 'subcategorynav_subcategorynav';
protected function _construct()
{
$this->setCollection($this->getCurrentChildCategories());
$parentCategory = $this->getParentCategory();
$parentAliasName = $parentCategory->getCategoryAlias();
$this->setAliasName($parentAliasName);
$this->setCategoryHeaderName($parentCategory->getName());
$this->addData(array(
'cache_lifetime' => 86400,
'cache_tags' => array(Vendor_CategoryNavigation_Block_Subcategorynav::CACHE_TAG,
Mage_Core_Model_Store_Group::CACHE_TAG),
));
}
public function getCacheKeyInfo()
{
$shortCacheId = array(
'SUBCATEGORYNAV_SUBCATEGORYNAV',
Mage::app()->getStore()->getId(),
Mage::getDesign()->getPackageName(),
Mage::getDesign()->getTheme('template'),
Mage::getSingleton('customer/session')->getCustomerGroupId(),
'template' => $this->getTemplate(),
'name' => $this->getNameInLayout(),
$this->getCurrentEntityKey()
);
$cacheId = $shortCacheId;
$shortCacheId = array_values($shortCacheId);
$shortCacheId = implode('|', $shortCacheId);
$shortCacheId = md5($shortCacheId);
$cacheId['entity_key'] = $this->getCurrentEntityKey();
$cacheId['short_cache_id'] = $shortCacheId;
/* Purpose: Including category id in cache block*/
$cacheId['category_path']=$this->getCurrenCategoryKey();
return $cacheId;
}
/**
* Retrieve current entity key
*
* @return int|string
*/
public function getCurrentEntityKey()
{
if (null === $this->_currentEntityKey)
{
$this->_currentEntityKey = Mage::registry('current_entity_key') ? Mage::registry('current_entity_key') : Mage::app()->getStore()->getRootCategoryId();
}
return $this->_currentEntityKey;
}
/**
* Retrieve current category key
*
* @return int|string
*/
public function getCurrenCategoryKey()
{
if (!$this->_currentCategoryKey)
{
$category = Mage::registry('current_category');
if ($category)
{
$this->_currentCategoryKey = $category->getPath();
} else {
$this->_currentCategoryKey = Mage::app()->getStore()->getRootCategoryId();
}
}
return $this->_currentCategoryKey;
}
/**
* Retrieve child categories of current category
*
* @return Varien_Data_Tree_Node_Collection
*/
public function getCurrentChildCategories()
{
$category = $this->getParentCategory();
/* @var $category Mage_Catalog_Model_Category */
$categories = $category->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Category_Collection */
$categories
->addAttributeToFilter('include_in_menu', 1)
->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('image')
->addAttributeToSelect('thumbnail')
->addAttributeToSelect('description')
->addAttributeToSelect('all_children')
->addAttributeToSelect('is_anchor')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('position', Varien_Db_Select:QL_ASC)
->addUrlRewriteToResult();
return $categories;
}
public function getParentCategory()
{
$currentCategory = Mage::getModel('catalog/layer')->getCurrentCategory();
$parentCategory = $currentCategory->getParentCategory();
return $parentCategory;
}
}
Template file:
<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
<?php foreach ($_categories as $_category): ?>
<?php if($_category->getIsActive()): ?>
<li>
<a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>>
<?php echo $this->escapeHtml($_category->getName()) ?>
</a>
</li>
<?php endif; ?>
<?php endforeach ?>
<?php endif; ?>