0

How to get category only product assign?

Code:-

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $categoryId = 4;?>
<?php $category = Mage::getModel('catalog/category')->load($categoryId) ?>
<?php $_categories = $category->getChildrenCategories() ?>
<?php if (count($_categories) > 0){ ?>
    <?php foreach($_categories as $_category){ ?>
        <h4 id="main_cat"><?php echo $_category->getName(); ?></h4>
        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) echo $_category;?>
        <?php if($_category->hasChildren()){ ?>
            <?php $_subcategories = $_category->getChildrenCategories() ?>
            <ul>
            <?php foreach($_subcategories as $_subcategory){ ?>
                <li>
                <label><span><?php echo $_subcategory->getName(); ?></span> </label>
                </li>
             <?php } //endforeach; ?>
            </ul>
         <?php } //end if ?>
    <?php } //end foreach  ?>
<?php } //end if ?>

I am not able to how to show only product assigned category.please help me?

2
  • Sorry, not clear what are you trying to do... can you try to explain it better? Nov 14, 2018 at 10:06
  • I want to show product assign category.because when i have click on any category its showing empty page.
    – user73619
    Nov 14, 2018 at 10:12

1 Answer 1

0
$cat = Mage::getModel('catalog/category')->load(4);
$subcats = $cat->getChildren();

foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid->getId);
  if($_category->getIsActive())
  {
    $caturl     = $_category->getURL();
    $catname     = $_category->getName();
    if($_category->getImageUrl())
    {
      $catimg     = $_category->getImageUrl();
    }
    echo '<h2><a href="'.$caturl.'" title="View the products for this category"><img src="'.$catimg.'" alt="" />'.$catname.'</a></h2>';
  }
}
?>
3
  • this is not working
    – user73619
    Nov 14, 2018 at 12:07
  • cache disable pls, and try again, i changed something. Nov 14, 2018 at 12:13
  • Maybe you'll want to change getURL() for getUrl() Nov 14, 2018 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.