Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How do I display subcategories inside view.phtml? 
 
winzippy
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-02-08
Rochester, NY, USA
 

I’ve tried the standard $this->getCategoryUrl($_category) method, but it doesn’t seem to work.

How can I access the categories from within view.phtml? Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
JTSchmidt
Jr. Member
 
Total Posts:  11
Joined:  2008-04-05
Irvine, CA
 

I don’t know if this will help you, but this will give you the current category from the view.phtml.  Im not sure how to get a hold of the other subcategories.

$category = $_product->getCategory();
echo $category->getURL();

 
Magento Community Magento Community
Magento Community
Magento Community
 
corbykissler
Member
 
Total Posts:  39
Joined:  2008-05-14
 

Here’s some code I used .. .

/* Get the categories that are active for the store */
$_main_categories=$this->getStoreCategories();
$_current_category=$this->getCurrentCategory();

?>
<ul>
<?php 
    
    
foreach ($_main_categories as $_main_category):
      if(
$_main_category->getIsActive()):                             
           
$cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
           
$layer Mage::getSingleton('catalog/layer');
           
$layer->setCurrentCategory($cur_category);     

/* Write the main categories */           
?>                
<li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></li>   


<?php 

/* Check the category variable loop against the current category path if it is - print sub categories */
if (in_array($this->getCurrentCategory()->getId(), $_categorypath)): ?>
<?php $_maincategorylisting
=$this->getCurrentCategory()?>                        
<?php $_categories
=$this->getCurrentChildCategories()?>
<?php 
if($_categories->count()):?>

<ul>
<? foreach ($_categories as $_category):?>                    
   <? 
if($_category->getIsActive()):                    
           
$cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
           
$layer Mage::getSingleton('catalog/layer');
           
$layer->setCurrentCategory($cur_subcategory);  
          
?>                          
<li><a href="<?php echo $this->getCategoryUrl($_category)?>"<?php echo $_category->getName()?></a></li>
    
<? endif;?>
<?endforeach?>
</ul>            
<?php /* This resets the category back to the original pages category
****     If this is not done, subsequent calls on the same page will use the last category
****    in the foreach loop
*/   
?>
<?php $layer
->setCurrentCategory($_maincategorylisting);  ?> 
<?endif
;?>   
 
<?endif
;?>                                   
                                                              
<?php         
endif;
endforeach;
 
Magento Community Magento Community
Magento Community
Magento Community
 
winzippy
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-02-08
Rochester, NY, USA
 

Thanks for the help guys, but I decied to try adding some code to View.php that would be called in view.phtml.

Here’s the code (which doesn’t work):

View.php:

...
    public function 
getCurrentChildCategories(){
        
return $this->getCurrentCategory()->getChildren();
    
}
...

view.pthml:

...
   
<?php $_categories$this->getCurrentChildCategories() ?>
<?php 
if($_categories->count()): ?>
    
    <?php 
foreach ($_categories as $_category): ?>
                        <?php 
if($_category->getIsActive()): ?>
                        
<li>
                            <
a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a> (<?php echo $_category->getProductCount() ?>)
                        </
li>
                        
<?php endif; ?>
                    <?php 
endforeach ?>
<?php 
endif; ?>
...

This is the error I get:

Fatal error: Call to a member function count() on a non-object in /home/ifolotwo/public_html/magento/app/design/frontend/viking/default/template/catalog/category/view.phtml on line 59

 
Magento Community Magento Community
Magento Community
Magento Community
 
MuschPusch
Member
 
Total Posts:  44
Joined:  2008-04-24
 

Hi,

i’m working on a xml output of all products and need to access the shop categories too.

First i initialize mage:

<?PHP

require_once 'magento/app/Mage.php';
umask(0);
//not Mage::run();
Mage::app('default');

//code snipped
$className Mage::getConfig()
->
getBlockClassName('catalog/product_list');
$block = new $className();
$className Mage::getConfig()
->
getBlockClassName('core/template');
$toolbar = new $className();
$block->setChild('toolbar'$toolbar);
//choose whatever category ID you want

$block->setCategoryId(3);
$block->setTemplate('catalog/product/list_xml.phtml');
echo 
$block->renderView();

?>

Then in the catalog/product/list_xml.phtml template i can access the loaded products:

$_productCollection=$this->getLoadedProductCollection();
foreach (
$_productCollection as $bla => $blabla){
 etc
... 
}

But how do i access the categories? I tried :

$_main_categories=$this->getStoreCategories();
print_r($_main_categories);
$_current_category=$this->getCurrentCategory();
print_r($_current_category);

But i think have do initialize/load them first.... HOW?!?

Thanks Volkan

 
Magento Community Magento Community
Magento Community
Magento Community
 
toadx
Jr. Member
 
Total Posts:  21
Joined:  2008-06-19
 

Did anyone ever solve this problem?  Or does anyone have any suggestions?  Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
MuschPusch
Member
 
Total Posts:  44
Joined:  2008-04-24
 

Yes, i solved it. It’s not really nice code but it works:

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo 
"<COLLECTION>\n";
// fetch category stuff
$collection Mage::getModel('catalog/category')->getTreeModelInstance()->getCollection();
foreach(
$collection as $category){
echo " <GROUP_>\n"
    echo 
" <NAME>".$category->getName()."</NAME>\n";
    
$get_category_image Mage::getModel('catalog/category')->load($category->entity_id);
    
    echo 
" <THUMB>".$get_category_image -> getImageUrl()."</THUMB>\n";
    echo 
" <LARGE>".$get_category_image -> getImageUrl()."</LARGE>\n";
    echo 
" <DESC>".$get_category_image->meta_description."</DESC>\n";
    echo 
" <CAT>".$get_category_image->meta_keywords."</CAT>\n";
    
$collection_products$category->getProductCollection();
    foreach (
$collection_products as $product){

        
echo " <ITEM>\n";
        
$product_data Mage::getModel('catalog/product')->load($product -> entity_id);
        echo 
"   <NAME>".$product_data->name."</NAME>\n";
        echo 
"   <THUMB>".$product_data->thumbnail."</THUMB>\n"
        echo 
"   <LARGE>".$product_data->image."</LARGE>\n"
        echo 
"   <COLOR>".$product_data->color."</COLOR>\n"
        echo 
"   <FABRIC>".$product_data->fabric."</FABRIC>\n"
        echo 
" </ITEM>\n";
    
}
echo " </GROUP_>\n"
}

echo "</COLLECTION>\n";

In my RSS reader i saw that there wil come a new web API and maybe you can solve it easier using 1.1....

 
Magento Community Magento Community
Magento Community
Magento Community
 
toadx
Jr. Member
 
Total Posts:  21
Joined:  2008-06-19
 
MuschPusch - 09 July 2008 01:13 AM

Yes, i solved it. It’s not really nice code but it works:


echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
echo 
"<COLLECTION>\n";
// fetch category stuff
$collection Mage::getModel('catalog/category')->getTreeModelInstance()->getCollection();
foreach(
$collection as $category){
echo " <GROUP_>\n"
    echo 
" <NAME>".$category->getName()."</NAME>\n";
    
$get_category_image Mage::getModel('catalog/category')->load($category->entity_id);
    
    echo 
" <THUMB>".$get_category_image -> getImageUrl()."</THUMB>\n";
    echo 
" <LARGE>".$get_category_image -> getImageUrl()."</LARGE>\n";
    echo 
" <DESC>".$get_category_image->meta_description."</DESC>\n";
    echo 
" <CAT>".$get_category_image->meta_keywords."</CAT>\n";
    
$collection_products$category->getProductCollection();
    foreach (
$collection_products as $product){

        
echo " <ITEM>\n";
        
$product_data Mage::getModel('catalog/product')->load($product -> entity_id);
        echo 
"   <NAME>".$product_data->name."</NAME>\n";
        echo 
"   <THUMB>".$product_data->thumbnail."</THUMB>\n"
        echo 
"   <LARGE>".$product_data->image."</LARGE>\n"
        echo 
"   <COLOR>".$product_data->color."</COLOR>\n"
        echo 
"   <FABRIC>".$product_data->fabric."</FABRIC>\n"
        echo 
" </ITEM>\n";
    
}
echo " </GROUP_>\n"
}

echo "</COLLECTION>\n";


In my RSS reader i saw that there wil come a new web API and maybe you can solve it easier using 1.1....

It sounds like this is code to give some kind of XML feed of all the items in the database.  Do you know how I could apply the above to show a listing of subcategories when someone views a category with no products in it?  Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
toadx
Jr. Member
 
Total Posts:  21
Joined:  2008-06-19
 

Any suggestions?  Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
MuschPusch
Member
 
Total Posts:  44
Joined:  2008-04-24
 

sorry i’m to busy at the moment… If you send me a PM next week i will have a look at it… Maybe someone else?

 
Magento Community Magento Community
Magento Community
Magento Community
 
toadx
Jr. Member
 
Total Posts:  21
Joined:  2008-06-19
 
MuschPusch - 14 July 2008 03:23 AM

sorry i’m to busy at the moment… If you send me a PM next week i will have a look at it… Maybe someone else?

Hey, don’t worry about it.  I just figured it out last night.  Thanks for your suggestions and the code you posted!

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

@toadx, could you post the code here for everyone else to see, or maybe write a wiki?

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
toadx
Jr. Member
 
Total Posts:  21
Joined:  2008-06-19
 
alistek - 14 July 2008 07:21 AM

@toadx, could you post the code here for everyone else to see, or maybe write a wiki?

-Adam

I actually solved it a while ago, so I don’t fully remember.  I think I was able to figure it out based on the code that MuschPusch posted.  I’ll post the code I used to do it below.  Probably not the cleanest solution, but it worked for me.  Good luck.

<?php
    $_product 
$this->getProduct();
    
$displayCategory $_product->getCategory();
    
    
//If no parent exists, show categories from the item's first category
    
if ($displayCategory === null)
    
{
        $categories 
$_product->getCategoryCollection()->setPage(11)->load();
        if (
$categories->count())
        
{
            $this
->setCategoryId(current($categories->getIterator()));
        
}
        
        $displayCategory 
Mage::getModel('catalog/category')->load($this->getCategoryId());
    
}
    
    
if ($displayCategory !== null)
    
{
        
if ($displayCategory->hasChildren() === false)
        
{
            $displayCategory 
$displayCategory->getParentCategory();
        
}
    
        $category 
Mage::getModel('catalog/category');
        
$tree $category->getTreeModel();
        
/* @var $tree Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree */
        
$nodes $tree->loadNode($displayCategory->getId())->loadChildren()->getChildren();
    
}
?>



    <?php
    
if ($displayCategory !== null)
    
{
    ?>
        
<h3><?php echo $displayCategory->getName(); ?></h3>
        <
ul>
            
<?php
                
foreach ($nodes as $_category)
                
{
                    $cur_subcategory 
Mage::getModel('catalog/category')->load($_category);
            
?>
                    
<li><a href="<?php echo $cur_subcategory->getUrl(); ?>"><?php echo $cur_subcategory->getName(); ?></a></li>
            
<?php
                }
            ?>
        
</ul>
    
<?php
    }
    ?>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top