Magento display categories and sub-categories
Magento display categories and sub-categories. Below code will show all the parent and child categories along with show/hide functionalities by jQuery.
<div class="block block-layered-nav"> | |
<div class="block-title"> | |
<strong><span><?php echo $this->__('Shop By Category') ?></span></strong> | |
</div> | |
<div class="block-content"> | |
<?php $productid = Mage::registry('current_product')->getId(); | |
$product = Mage::getSingleton('catalog/product')->load($productid); | |
$parentIds = $product->getCategoryIds(); | |
$parentId = $parentIds[0]; | |
$_categories = Mage::getBlockSingleton('catalog/navigation'); | |
foreach ($_categories->getStoreCategories() as $_category) { | |
$category = Mage::getModel('catalog/category'); | |
$category->load($_category->getId()); | |
$subcategories = explode(',', $category->getChildren()); | |
if(!in_array($_category->getId(),$parentIds)) { $hide="display:none"; $inactive="inactive"; } else { $hide=""; $inactive="active"; } | |
?> | |
<div style="padding:5px 5px 0px 10px"> | |
<div class="cat parent <?php echo $inactive;?>" style="font-size:15px"><?php echo $_category->getName() ?></div> | |
<div class="child" style="<?php echo $hide;?>"> | |
<?php foreach ($subcategories as $subcategoryId) { | |
$category->load($subcategoryId); | |
if($category->getChildren() == '') { | |
if(in_array($subcategoryId,$parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; } | |
echo '<div class="subcat" style="'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>'; | |
} else {?> | |
<div class="subcat"> | |
<div class="parent active"><?php echo $category->getName() ?></div> | |
<div class="child"> | |
<?php $subsubcategories = explode(',', $category->getChildren()); | |
foreach($subsubcategories as $subsubcatid) { | |
if(in_array($subsubcatid, $parentIds)) { $bold = "font-weight:bold"; } else { $bold = ""; } | |
$category->load($subsubcatid); | |
echo '<div style="padding-left:10px;'.$bold.'"><a href="' . $category->getURL() . '">' . $category->getName() . '</a></div>'; | |
} ?> | |
</div> | |
</div> | |
<?php } | |
} | |
?> | |
</div> | |
</div> | |
<?php | |
} ?> | |
</div> | |
</div> | |
<script type="text/javascript"> | |
jQuery('.block-content .cat').click(function(){ | |
var t = jQuery(this); | |
if(jQuery(this).next().css('display')=='none') { | |
jQuery('.col-left .block-content .child').hide(); | |
t.next().show(); t.next().find('div.child').show("slow"); | |
} else { | |
t.next().hide(); t.next().find('div.child').hide("slow"); | |
} | |
}); | |
</script> |
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress