2

This question already has an answer here:

I am working on script which is used in outside of magento. I am using this link for creating category tree structure but it gives me only those category collection which are active in admin section for particular store. It not gives me other categories which are active in subadmin panel but inactive in admin panel. I want all active category tree structure on the basis of subadmin active condition on categories.

marked as duplicate by Richard, mbalparda, Qaisar Satti, Keyul Shah, David Manners Nov 20 '15 at 13:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

first of all if you are outside magento call Mage file in your current file, then you can use all models of magento.

Use bellow code to show all categories, the bellow code is showing me 3 level categories:

 <?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php //$currentCategory = Mage::registry('current_category') ?>

<?php
$currentCat = Mage::registry('current_category');
if ($currentCat != '') {
        $categoryp = Mage::getModel('catalog/category')->load($currentCat->getId());
        $path = $categoryp->getPath();  

        $ids = explode('/', $path);
    $var_id = $currentCat->getId();
} 


?>
<?php $m = 0; ?>
<?php if (count($_categories) > 0): ?>
    <ul >
        <?php foreach ($_categories as $_category): $m++; ?>

            <li class="<?php if ($var_id == ($_category->getId())) {
                echo 'active';
            } ?>" >

            <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><img src="<?php echo $this->getSkinUrl('images/menu_icon_' . $m . '.png'); ?>" alt="">
            <?php echo $_category->getName() ?></a>
            <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>

            <?php $_subcategories = $_category->getChildrenCategories() ?>
            <?php if (count($_subcategories) > 0): ?>                    
                <ul>                            
                    <li>
                        <div class="submenu_area">
                            <div class="categorys">
                                <ul id="inner_category">
                                    <?php foreach ($_subcategories as $_subcategory): ?>
                                    <?php
                                        //echo Mage::getBaseUrl('media').'catalog/category/'.Mage::getModel('catalog/category')->load($_subcategory->getId())->getThumbnail();
                                    ?>
                                    <?php $_subcategory = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>

                                    <?php $_subsubcategories = $_subcategory->getChildrenCategories() ;?>
                                    <?php $subcount = $_subsubcategories->count(); ?>
                                    <li class="<?php if($subcount < 1){echo 'nochild';}?><?php if(isset($ids[3]) && ($ids[3] == $_subcategory->getId() or $ids[2] == $_subcategory->getId()) ){echo 'current';}?>">
                                        <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                            <?php echo $_subcategory->getName() ?>
                                        </a>
                                        <?php if($subcount > 0 ){?>
                                        <ul> 
                                            <li>
                                                <div class="subcategory_labels">
                                                    <div class="subcategory_menu">
                                                    <h2><?php echo $_subcategory->getName(); ?> </h2>
                                                        <!--sub sub category-->
                                                    <?php

                                                    if ($subcount > 4) {
                                                        $CatPerColumn = ceil($subcount / 4);
                                                    } else {
                                                        $CatPerColumn = 1;
                                                    }
                                                    ?>
                                                    <?php $i = 1;
                                                    if (count($_subsubcategories) > 0): ?>
                                                        <?php foreach ($_subsubcategories as $_subsubcategory): ?>    
                                                              <?php if (($i - 1) % $CatPerColumn == 0): ?>
                                                            <div class="subcat_menus_list">
                                                            <?php endif; ?>

                                                            <!--sub fourth sub category-->
                                                            <?php $_subcategoryfou = Mage::getModel('catalog/category')->load($_subsubcategory->getId()) ?>
                                                            <?php $_subsubcategoriesfou = $_subcategoryfou->getChildrenCategories(); ?>
                                                            <?php if ($_subsubcategoriesfou->getSize() >  0) { ?>
                                                                <h4>
                                                                    <a href="<?php echo $_helper->getCategoryUrl($_subsubcategory) ?>">
                                                                        <?php echo $_subsubcategory->getName() ?>
                                                                    </a>
                                                                </h4>
                                                            <?php  }else{ ?>
                                                                <h4 class="hsmall">
                                                                    <a href="<?php echo $_helper->getCategoryUrl($_subsubcategory) ?>">
                                                                        <?php echo $_subsubcategory->getName() ?>
                                                                    </a>
                                                                </h4>
                                                            <?php }  ?>
                                                            <?php if (count($_subsubcategoriesfou) > 0): ?>
                                                            <ul>
                                                                <?php foreach ($_subsubcategoriesfou as $_subsubcategory): ?>
                                                                <li>
                                                                    <a href="<?php echo $_helper->getCategoryUrl($_subsubcategory) ?>">
                                                                         <?php echo $_subsubcategory->getName() ?>
                                                                    </a>
                                                                </li> 

                                                                <?php endforeach; ?>   
                                                            </ul>

                                                            <?php endif; ?>
                                                            <!--sub sub category-->

                                                            <?php if ($i % $CatPerColumn == 0 Or $i == $subcount): ?>
                                                            </div><?php endif; ?>
                                                            <?php $i++;
                                                        endforeach; ?>           
                                                    <?php endif; ?>                                 
                                                    <div class="clr"></div>
                                                </div>


                                                </div>
                                            </li>

                                         </ul>
                                    <?php } ?>
                                    </li>
                                    <?php endforeach; ?>
                                </ul>

                            </div>


                            </div>
                        </li>
                    </ul>
            <?php endif; ?>    
            </li>
        <?php endforeach; ?>


    </ul>
  • Where did you checked if the category is enabled or disabled – Vishwas Bhatnagar Nov 20 '15 at 10:01
  • this is for all active/inactive categories, if you need use this code <?php if($_category->getIsActive()): ?> – Surya prakash Patel Nov 20 '15 at 10:07
0
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";
Mage::app('1');
$collection= Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addAttributeToSelect('id')
->addAttributeToSelect('parent_id')
->addAttributeToSelect('position')
->addAttributeToSelect('level')
->addAttributeToSelect('is_active');
echo "<table border='1' width='100%'>";
    echo "<tr>";
    echo "<th>id</th>";
    echo "<th>name</th>";
    echo "<th>parent</th>";
    echo "<th>is_active</th>";
    echo "<th>position</th>";
    echo "<th>level</th>";
echo "</tr>";

foreach($collection as $col)
{
    echo "<tr>";
            echo "<td>".$col->getId()."</td>";
            echo "<td>".$col->getName()."</td>";
            echo "<td>".$col->getParentId()."</td>";
            echo "<td>".$col->getIsActive()."</td>";
            echo "<td>".$col->getPosition()."</td>";
            echo "<td>".$col->getLevel()."</td>";
    echo "</tr>";
}
echo "</table>";
?>

Edited the Demo for you

  <?php
    $rootcatId= Mage::app()->getStore()->getRootCategoryId();
    $categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
    function  get_categories($categories) {
        $array= '<ul>';
        foreach($categories as $category) {
            $cat = Mage::getModel('catalog/category')->load($category->getId());
if ($cat->getIsActive()) {

            $count = $cat->getProductCount();
            $array .= '<li>'.
            '<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
                      $category->getName() . "(".$count.")</a>\n";
            if($category->hasChildren()) {
                $children = Mage::getModel('catalog/category')->getCategories($category->getId());
                 $array .=  get_categories($children);
                }
             $array .= '</li>';
        }
}
        return  $array . '</ul>';
    }
    echo  get_categories($categories); ?>
  • It provides Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Category_Collection::getStoreCategories() – Mathew Nov 20 '15 at 9:24
  • is flat categories are enabled ? – Vishwas Bhatnagar Nov 20 '15 at 9:27
  • no it is not enabled. – Mathew Nov 20 '15 at 9:31
  • Sorry My bad , edited my Answer – Vishwas Bhatnagar Nov 20 '15 at 9:59
  • 1
    @VishwasBhatnagar getStoreCategories is defined in Mage::helper('catalog/category') – Richard Nov 20 '15 at 10:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.