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

Page 1 of 4
Categories on the homepage - someone please help! 
 
rzazueta
Member
 
Total Posts:  34
Joined:  2008-07-21
 

Magento looks so great on the surface, but is unbearably difficult to customize given the lack of coherent documentation and the fact that no one seems to have straight answers. So I’m posting this question in a last ditch effort in the hopes that someone can actually help. PLEASE don’t point me to the designers guide as it’s too high-level a document to help with real customization I don’t need a programming philosophy lesson, I need answers.

I have created a new template based on the 3 column design found in template/page/3column.phtml and have successfully set up the store to point to that template and use it on the homepage. It has the same variable calls in it, so the page.xml layout file should still be viable. I’ve created two categories under the Default Category in the admin and added products with quantities greater than 0 to them. I’ve ensured that the default category is the root category in the “Manage Stores” section of the admin. Even with doing all of this, I can not get a single category to show up on the homepage. In addition, I’m completely confused as to why the callouts are even on the homepage - where do I define what to show in the columns on the homepage?

What I want is a list of categories showing up in the left column - a seemingly simple request made near impossible by the lack of resources explaining how to make this work. Please, please help - every search on Google and in these forums has produced either incomplete code that produces no results or more people asking the exact same questions with no answers. Every conversation on the chat board has been circular and gone nowhere. Please shed some light on this topic and help the community out.

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rzazueta
Member
 
Total Posts:  34
Joined:  2008-07-21
 

A couple of other things…

I turned off the system cache, so changes show up fine. I figured out my question about the callouts (though I’m still unclear on how I could change that on a per-page or per-section basis - i.e. why do I need the left_col callout on every page - what if I want something different for the category main pages?). I changed the left column so that it points to catalog/navigation/left.phtml, but now it’s generating the PHP error “Call to a member function count() on a non-object in C:\working\magento\public_html\app\design\frontend\boardsnmore\default\template\catalog\navigation\left.phtml on line 29”. Looking at the code, this is being caused by a call to the method getCurrentChildCategories() on $this - I’m unclear, though, as to what $this represents. Given that left.phtml is essentially an include file, what object is being instantiated that I’m using $this to refer to it?

Any help here?

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rzazueta
Member
 
Total Posts:  34
Joined:  2008-07-21
 

Update: I’ve modified the catalog.xml (this is where the callouts were being called) so that the left callout is replaced with the following:

<reference name="left">
            <
block type="catalog/navigation" name="catalog.left" template="catalog/navigation/left.phtml"/>
        </
reference>

Now I get the PHP error: “Fatal error: Call to a member function getChildren() on a non-object in C:\working\magento\public_html\app\code\core\Mage\Catalog\Block\Navigation.php on line 77”

Looking at the code, the getChildren() method is being called on an apparent category objectinstantiated by the following two lines of code:

$layer Mage::getSingleton('catalog/layer');
        
$category   $layer->getCurrentCategory();

It looks like $category is the boolean false in this situation. What’s happening here? Why is the layer being called? What is its purpose? And why is it returning false for the getCurrentCategory() method? Shouldn’t it return a reference to the root category defined in store management?

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rzazueta
Member
 
Total Posts:  34
Joined:  2008-07-21
 

Looking through the code to fix the prior errors, I saw there was a method that appears to be intended to grab all fot je categories for the current store. So, in Navigation.php, I put an if-then around the code instantiating the $category variable that was turning out to be false. Here’s the new code, starting at line 66 in app/code/core/Mage/Catalog/Blobk/Navigation.php:

public function getCurrentChildCategories()
    
{
        $layer 
Mage::getSingleton('catalog/layer');
        
//RDZ - Added to allow the display of categories on the homepage.
        
if(($category   $layer->getCurrentCategory()) === false{
            
return $this->getStoreCategories();
        
}

This actually successfully displayed the second level categories (everything beneath default category) in my assigned root category on the homepage using template/catalog/navigation/left.phtml assigned to the left reference in cms.xml instead of the left_col callout.

And now I have a shiny new error. When I click on one oif the categories with an item assigned to it, I get the following PHP error:

Fatal error: Call to undefined method Mage_Catalog_Helper_Product::getPriceHtml() in C:\working\magento\public_html\app\design\frontend\boardsnmore\default\template\catalog\product\list.phtml on line 100

How in the world has anyone been able to get this system working?

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rzazueta
Member
 
Total Posts:  34
Joined:  2008-07-21
 

Well, since no one’s rushed to my aid (or anyone else’s for that matter - c’mon folks, if you find a solution don’t leave it in PMs - this is supposed to be an open source community) I’ve been struggling with this myself. The only solution I’ve come up with involves directly changing the app code. Here’s hoping a new upgrade doesn’t overwrite these changes.

Recall my goal was to display the children from a specific category on the homepage. Why I want to do this is irrelevant - it’s a customization issue that should just exist - but I was able to fiond a solution.

First, I added a new method in the app/code/core/Mage/Block/Navigation.php:

public function getChildCategories($categoryId)
    
{
          $category 
Mage::getModel('catalog/category');
          if(
$category->checkId($categoryId) === false{
              
return false;
          
}
        $layer 
Mage::getSingleton('catalog/layer');
        
$category->load($categoryId);
        
$layer->setCurrentCategory($category);
        
/* @var $category Mage_Catalog_Model_Category */
        
$collection Mage::getModel('catalog/category')->getCollection();
        
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
        
$collection->addAttributeToSelect('url_key')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('is_anchor')
            ->
addAttributeToFilter('is_active'1)
            ->
addIdFilter($category->getChildren())
            ->
joinUrlRewrite()
            ->
load();

        
//$productCollection = Mage::getResourceModel('catalog/product_collection');
        //$layer->prepareProductCollection($productCollection);
        //$productCollection->addCountToCategories($collection);
        
return $collection;
    
}

This takes the numeric category ID and returns a collection of categories that can be used in, say, template/catalog/navigation/left.phtml like so:

<?php $_categories=$this->getCurrentChildCategories() ?>
<?php 
//$_categories=$this->getChildCategories(3) ?>
<?php if($_categories->count()) { ?>
<div class="box layered-nav">
    <
div class="border-creator">
        <
div class="narrow-by">
            <
ul>
                
<?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>
                        </
li>
                    
<?php endif; ?>
                <?php 
endforeach ?>
            
</ul>
        </
div>
    </
div>
</
div>
<?php 
    }
?>

This should display the children for numerical ID 3 as an unordered list in the left hand column of your page, assuming you set it up that way in your layouts. To get the catalog ID, log in as an admin, go to “Catalog->Manage Categories” and click on the category whose children you want to display. You MUST choose a category with children, or this just won’t work. The numeric ID appears in the URL like so:

/index.php/admin/catalog_category/edit/id/3/

see that “3” in the URL after id? That’s the numeric ID you’re looking for.

So, even with all this setup, the puzzle still didn;t work, and I’m still not clear as to why I had to do this following part. It looked like calling the specific category in the Navigation was doing the job - a call to getName() returned the correct name - but the call to getChildren returned an empty string. Looking through the code, the Category’s Tree class does get called, but it never gets loaded. So I modified the code in app/code/core/Mage/Catalog/Model/Category.php near line 101 in the method “getTreeModelInstance()”:

public function getTreeModelInstance()
    
{
        
if (is_null($this->_treeModel)) {
            $this
->_treeModel Mage::getResourceSingleton('catalog/category_tree');
            
//RZ - Not even sure why this isn't being called like it should be. Adding it.
            
$this->_treeModel->load();
        
}
        
return $this->_treeModel;
    
}

That loaded the category tree structure so that the call to getChildren() finally produced something other than a blank string.

So, we have success on this front. I’m tossing some karma back into the community pool in the hopes that others may finally post answers to the deluge of questions and problems people are expressing on this board.

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
craigboi87
Sr. Member
 
Avatar
Total Posts:  90
Joined:  2008-07-18
 

rite dude i am lost here! what are you trying to achieve?  you want categories down the left hand side?

 
Magento Community Magento Community
Magento Community
Magento Community
 
craigboi87
Sr. Member
 
Avatar
Total Posts:  90
Joined:  2008-07-18
 

had the same problem hope this helps. its the most clear and simple way of doing it (:

1. Create a file named “left_menu1.phtml” in “magento\app\design\frontend\default\default\template\catalog\navigation\”

left_menu1.phtml

<div class="box layered-nav">
    <
div class="head">
      <
h3><?php echo $this->__('Browse By'?></h3>
    </
div>
    <
div class="border-creator">
      <
div class="narrow-by">
           <
dl id="narrow-by-list">
           
         <
dd>
         <
ol>        
        
<?php foreach ($this->getStoreCategories() as $_category): ?>
            
<dt>
             <
a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
            </
dt>
        
<?php endforeach ?>
        
       
</ol>
      </
dd>
      </
dl><script type="text/javascript">decorateDataList('narrow-by-list')</script>
    
</div>
   </
div>
</
div>
2. Go to: CMS->Manage Pages, and click in Homepage, select in the left “Custom Design” and write this code in “Layout Update XML”:
<reference name="left">
    <
block type="catalog/navigation" name="catalog.leftnavig" before="-" template="catalog/navigation/left_menu1.phtml"/>
</
reference>
3. save it

to be honest i think this should be a sticky somewhere as its got to be a common ass question (:

 
Magento Community Magento Community
Magento Community
Magento Community
 
mart3ndo
Jr. Member
 
Total Posts:  13
Joined:  2008-08-14
 

Thanks, craigboi87

I managed to get that working on my site, i want to add though that i could only get it working by using the three column design

On an unrelated topic, does anyone know how to get products to show up on the front page, like Featured Products, New Products, stuff like that. There’s probably already a post somewhere, but i cant find any,

Thanks,

Martyn

 
Magento Community Magento Community
Magento Community
Magento Community
 
rzazueta
Member
 
Total Posts:  34
Joined:  2008-07-21
 

I specifically was trying to get the children of a category showing up on the homepage. I wanted to display our store as two separate views at the same time - a list of regular departments (i.e. Men’s Apparel, Women’s Apparel, Accessories, etc.) and a list of brands (O’Neill, Volcom, etc.). The only solution that made sense was to create two categories under the Default Category - “Shop By Brand” and “Shop By Department” - and pull the children only out of the relevant on (i.e. “Shop by Brand” has categories named “O’Neill”, “Volcom”, etc.). I just wanted the brand names as a list on the homepage. So my code above is a bit more advanced than the simple “Show categories on the homepage” a lot of folks had been asking for. But the solution posted for that ought to work - it did for me before I took the next step of figuring this subcat issue out.

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rzazueta
Member
 
Total Posts:  34
Joined:  2008-07-21
 

Actually, Craig, adding your post to the wiki documentation may be better than using a sticky. I’m finding the support here frustrating given that, as you mentioned, tons of people ask for help on similar problems and no one seems eager to help.

I want to help with the request regarding featured and new items, but I’m still wrapping my head around that. Having said that, here’s a post I found in my research that may help, at least with the featured items:

http://www.codeindoses.com/2008/08/magento-home-page-custom-attribute/

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jhutton
Jr. Member
 
Total Posts:  19
Joined:  2008-06-24
 

Rob,

Big thanks, dude. I was trying to get my subcategories to show up during product views, and I couldn’t figure out why $category->getChildren() wasn’t returning anything.

Now in my view I just do:

<?php
        
if (!Mage::registry('product') && $this->getProductId()) {
            $product 
Mage::getModel('catalog/product')->load($this->getProductId());
            
Mage::register('product'$product);
        
}
        $_product 
Mage::registry('product');
?>

<?php 
if ($_product): ?>
<?php
    
// get last id in category path for current category
    
$category   Mage::getModel('catalog/category')->load($_path[count($_path) - 1]);
    
$children_ids implode(','Mage::getResourceSingleton('catalog/category_tree')->load()->getChildren($category->getId(), false));

    
$col Mage::getModel('catalog/category')->getCollection();

    
$col->addAttributeToSelect('url_key')
      ->
addAttributeToSelect('name')
      ->
addAttributeToSelect('is_anchor')
      ->
addAttributeToFilter('is_active'1)
      ->
addIdFilter($children_ids)
      ->
joinUrlRewrite()
      ->
load();

    
$_categories $col;
?>

    <?php 
if (count($_categories) > 0): ?>
    
<ul class="sub_nav">
      
<?php foreach ($_categories as $_category): ?>

etc, case anybody is interested. Don’t even have to hack the core.

 
Magento Community Magento Community
Magento Community
Magento Community
 
StarGumbo
Jr. Member
 
Total Posts:  7
Joined:  2008-02-14
 

Rob,

Thank you much for this addition.  I had been trying to get subcategory info to display on the home page and found this to be a great solution.

If anyone is interested I added this to display the images and descriptions of the categories as well -

in app/code/core/Mage/Catalog/Block/Navigation.php I added the ‘image’ and ‘description’ attributes

public function getChildCategories($categoryId)
        
{
              $category 
Mage::getModel('catalog/category');
              if(
$category->checkId($categoryId) === false{
                  
return false;
              
}
            $layer 
Mage::getSingleton('catalog/layer');
            
$category->load($categoryId);
            
$layer->setCurrentCategory($category);
            
/* @var $category Mage_Catalog_Model_Category */
            
$collection Mage::getModel('catalog/category')->getCollection();
            
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
            
$collection->addAttributeToSelect('url_key')
                ->
addAttributeToSelect('name')
                
/* Add the Image and Description Attributes */
                
->addAttributeToSelect('image')
                ->
addAttributeToSelect('description')
                
/* End Edit */
                
->addAttributeToSelect('is_anchor')
                ->
addAttributeToFilter('is_active'1)
                ->
addIdFilter($category->getChildren())
                ->
joinUrlRewrite()
                ->
load();
    
            
//$productCollection = Mage::getResourceModel('catalog/product_collection');
            //$layer->prepareProductCollection($productCollection);
            //$productCollection->addCountToCategories($collection);
            
return $collection;
        
}

and in my custom category-display .phtml file ( app/design/frontend/default/reskin/template/catalog/navigation/catnav-home.phtml )

<div class="cat-nav-table">
<?php foreach ($this->getChildCategories(6) as $_childCategory): ?>
        
<div class="cat-image-container">
            <
div class="imageDiv">
            <
h5 style="margin-bottom:0;"><?php echo $_childCategory->getName() ?></h5>
            <
a href="<?php echo $this->getCategoryUrl($_childCategory) ?>"<?php if ($this->isCategoryActive($_childCategory)): ?> class="activeNav"<?php endif ?>>
            <
img src="<?php echo $_childCategory->getImageUrl() ?>" alt="<?php echo $_childCategory->getName() ?>" width="75" height="75"/>
            </
a>
            <
p><?php echo substr($_childCategory->getDescription(),0,50?><?php echo '...' ?></p>
            <
class="view-catalog-link" href="<?php echo $this->getCategoryUrl($_childCategory) ?>">View Our Catalog ></a>
            </
div>
        </
div>
<?php endforeach; ?>
</div>

which is referenced in the ‘custom design’ tab for the home page with

<block type="catalog/navigation" name="catalog.catnav-home" template="catalog/navigation/catnav-home.phtml"/>

All will need to be styled with css, but works like a charm

Tony

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

You might be interested in a few tutorials I’ve written

1) Adding or removing callouts in the right/left column: http://www.getfido.net/blog/magento/moving-removing-callouts-on-the-left-right-columns.php

2) Editing the Magento navigation http://www.getfido.net/blog/magento/editing-the-navigation.php
This 2nd one will show you how magento retrieves the categories and displays them (and sub categories) for the main navigation and the layered land hand navigation.

As for your original problem of categories not showing up, I assume you meant that no matter what settings you did, you couldn’t get the main navigation to appear?

Edit: After reading your posts more thoroughly, I can see that you probably don’t need to read my posts to help you as you’ve identified the files you need for your customization.

My only suggestion then is to move your changes out of the core code.

You can easily do this by duplicating the folder structure in your LOCAL folder and adding the files you edited.

So if you edit:
app/code/core/Mage/Catalog/Block/Product/View.php

you can save your edited version to override the original View.php here:
app/code/local/Mage/Catalog/Block/Product/View.php

The suggested way of doing this is to overwrite the Block via your own custom module. (There are many threads along this topic)

 
Magento Community Magento Community
Magento Community
Magento Community
 
den_piero
Jr. Member
 
Total Posts:  9
Joined:  2008-09-14
Jakarta, Indonesia
 

Star Gumbo.....Nothing happen! is it must be styled first with css, so the sub categories can display? or something wrong

 
Magento Community Magento Community
Magento Community
Magento Community
 
squirrels
Sr. Member
 
Total Posts:  93
Joined:  2008-08-27
 

Wow, thank you so much for this!
Have been trying the entire day to make layered navigation turn up on the front page but without luck… Now at least I can have the Categories on the frontpage!

Does anyone have an idea how to make all attributes show up as well? (My goal is still to have a proper layered navigation on the frontpage without having to make a core hack)

 
Magento Community Magento Community
Magento Community
Magento Community
 
squirrels
Sr. Member
 
Total Posts:  93
Joined:  2008-08-27
 

@jhutton

jhutton - 04 September 2008 09:18 AM

Rob,

Big thanks, dude. I was trying to get my subcategories to show up during product views, and I couldn’t figure out why $category->getChildren() wasn’t returning anything.

Now in my view I just do:

etc, case anybody is interested. Don’t even have to hack the core.

But in what file do you do it? There are several view.phtml and being quite new at this I can’t identify which one you are referring to…
Very curious at the output of this! smile

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 4