Magento

eCommerce Software for Online Growth

Magento Forum

From setting up your store to managing your products, pages and promotions to generating detailed reports, the Magento User Guide empowers the user to utilize the platform for all of its vast capacity.
Available in eBook and Print formats – Download Now!!!
   
Page 1 of 8
SOLVED: Almost there - categories on homepage working but images aren’t showing up
 
spib
Jr. Member
 
Total Posts:  7
Joined:  2008-07-26
 

I’ve written some code to display all my categories on the homepage. For each category I select a single random product from that category and then display it as a thumbnail with the category name.

This all works except for some reason the image isn’t being pulled through, just the placeholder is displayed. Any idea what I might be missing?

Thanks

James

<?php 

    
// Iterate all categories in store
    
foreach ($this->getStoreCategories() as $_category):
        
        
// If category is Active
        
if($_category->getIsActive()):

            
// Load the actual category object for this category
            
$cur_category Mage::getModel('catalog/category')->load($_category->getId());
                    
            
// Load a random product from this category
            
$products Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category);
            
$products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);
            
            
$products->load();
            
            
// This a bit of a fudge - there's only one element in the collection
            
$_product null;
            foreach ( 
$products as $_product {}
            
            ?>
            
            
<div style="float: left; padding-right: 30px; text-align: center;">
                <
div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_category)?>">
            
            
<?php
            
if(isset($_product)):
            
?>
            
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
            
<?php
            
endif;
            
?>
                
</div>
                
<?php echo $_category->getName()?></a></p>
            </
div>
            
<?php
        
endif;
    endforeach;
?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
spib
Jr. Member
 
Total Posts:  7
Joined:  2008-07-26
 

Finally figured this out myself. By default, it doesn’t seem to fetch any attributes when you load a product so you have to explicitly request them in the query using addAttributeToSelect(’small_image’). Fixed code shown below

<?php 

    
// Iterate all categories in store
    
foreach ($this->getStoreCategories() as $_category):
        
        
// If category is Active
        
if($_category->getIsActive()):

            
// Load the actual category object for this category
            
$cur_category Mage::getModel('catalog/category')->load($_category->getId());
                    
            
// Load a random product from this category
            
$products Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category)->addAttributeToSelect('small_image');
            
$products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);
            
            
$products->load();
            
            
// This a bit of a fudge - there's only one element in the collection
            
$_product null;
            foreach ( 
$products as $_product {}        
            ?>
            
            
<div style="float: left; padding-right: 30px; text-align: center;">
                <
div class="linkimage"><p><a href="<?php echo $this->getCategoryUrl($_category)?>">
            
            
<?php
            
if(isset($_product)):
            
?>
            
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
            
<?php
            
endif;
            
?>
                
</div>
                
<?php echo $_category->getName()?></a></p>
            </
div>
            
<?php
        
endif;
    endforeach;
?>

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

I’m still trying to get a grasp on how the code is architected here. What object does the “$this” in your call for all categories reference? I see you’re grabbing products from a category, but what if the category is a parent category to a list of children - where in the code can I find that method?

Thanks!

Rob Z.

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

So, just to see how you’re code is working, I replaced the data in the tmeplates/callouts/left_col.phtml file with yours. If I do it straight away, I get an error that the first foreach received a bad variable to work from. So I encased the whole thing in an if-then to test if it’s an array and, if not, show me what the variable you get from $this->getStoreCategories() is, which turns out to be Null. So, in short, that method is returning null for me. I have created a couple of categories and populated them with products and ensured the “Is_active” property is set to “Yes”, but this still returns Null. Any idea what I can do fix this?

Thanks!

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
schmelzgeist
Sr. Member
 
Total Posts:  92
Joined:  2008-05-27
Marzipane World Capital (Lübeck, Germany)
 

hmm. it works but i have only 1 Categorie als Categorie how do i display the child categories?

 Signature 

WE have no future because our Present is too volatile.We have only riskmanagement.
The spinning of the given moment´s scenario. *Pattern Recognition.W.Gibson*

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

I can;t even get a single category to show, even though I;ve created categories both at the root level and under the default category,, populated with active products with a quantity of grater than 10. Where is this code supposed to go and how am I supposed to make it work? And why in the heck doesn’t Magento make this easy?

Rob Z.

 
Magento Community Magento Community
Magento Community
Magento Community
 
spib
Jr. Member
 
Total Posts:  7
Joined:  2008-07-26
 

Hey Guys,

Sorry for not responding, didn’t get any notifications that there had been replies to this thread.

OK, so this code should go in a new file called /magento/app/design/frontend/default/default/template/catalog/category/list.phtml

Then, in your homepage CMS module, enter the following

{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}}

Note that this code is only designed to show the top level categories of the store - it won’t iterate into sub-categories.

 
Magento Community Magento Community
Magento Community
Magento Community
 
itnavad
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2008-09-08
Osimo
 

Hi, maybe someone can help me:
how can I display the picture’s category instead of the product?

 Signature 

In the world there are only 10 kinds of people: those who know the binary code and who does not!

 
Magento Community Magento Community
Magento Community
Magento Community
 
itnavad
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2008-09-08
Osimo
 

I’ve resolved it!
this is the code that i used in list.phtml to display categories’ images:

<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $open 
$this->isCategoryActive($_category); ?>
<?php
$cur_category
=Mage::getModel('catalog/category')->load($_category->getId());
$layer Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
if (
$immagine $this->getCurrentCategory()->getImageUrl()):
?>     

<div style="float: left; padding-right: 30px; text-align: center;">
                <
div class="linkimage">
                    <
p>
                    <
a href="<?php echo $this->getCategoryUrl($_category)?>">
                    <
img src="<?php echo $immagine ?>" alt="<?php echo $this->htmlEscape($this->getCurrentCategory()->getName()) ?>" width="135" height="135" />
                    
<?php echo $_category->getName()?>
                    
</a>
                    </
p>
                </
div>
</
div>

<?php endif; ?>
<?php 
endforeach; ?>

 Signature 

In the world there are only 10 kinds of people: those who know the binary code and who does not!

 
Magento Community Magento Community
Magento Community
Magento Community
 
itnavad
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2008-09-08
Osimo
 

I’ve resolved it!
this is the code that i used in list.phtml to display categories’ images:

<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $open 
$this->isCategoryActive($_category); ?>
<?php
$cur_category
=Mage::getModel('catalog/category')->load($_category->getId());
$layer Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
if (
$immagine $this->getCurrentCategory()->getImageUrl()):
?>     

<div style="float: left; padding-right: 30px; text-align: center;">
                <
div class="linkimage">
                    <
p>
                    <
a href="<?php echo $this->getCategoryUrl($_category)?>">
                    <
img src="<?php echo $immagine ?>" alt="<?php echo $this->htmlEscape($this->getCurrentCategory()->getName()) ?>" width="135" height="135" />
                    
<?php echo $_category->getName()?>
                    
</a>
                    </
p>
                </
div>
</
div>

<?php endif; ?>
<?php 
endforeach; ?>

 Signature 

In the world there are only 10 kinds of people: those who know the binary code and who does not!

 
Magento Community Magento Community
Magento Community
Magento Community
 
brendan.
Guru
 
Total Posts:  726
Joined:  2008-07-16
London, United Kingdom
 

hi itnavad,

thanks for the code, works great. Ive been looking for something like this for a while

cheers
Brendan

 Signature 

magento 1.3.1.2
PHP Version 5.2.6
http://www.getstuff.com.au

 
Magento Community Magento Community
Magento Community
Magento Community
 
brendan.
Guru
 
Total Posts:  726
Joined:  2008-07-16
London, United Kingdom
 

ok, question,

This works great, but how would i get the subcategories also? Can anyone suggest an idea?

thanks

 Signature 

magento 1.3.1.2
PHP Version 5.2.6
http://www.getstuff.com.au

 
Magento Community Magento Community
Magento Community
Magento Community
 
jrutter
Jr. Member
 
Total Posts:  24
Joined:  2008-04-08
 

Does anyone know how to take this code and instead of showing the category and a random image? Show the category with all the products listed underneath it for each category??

Thanks!
Jake

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
Total Posts:  53
Joined:  2008-06-26
 

I followed the steps but for some reason it didnt work for me:( Is there anything specific other than adding images to categories I need to do?

 
Magento Community Magento Community
Magento Community
Magento Community
 
brendan.
Guru
 
Total Posts:  726
Joined:  2008-07-16
London, United Kingdom
 

davinder,

have you tried just returning the category data without images. I would try this first to validate the code is actaully pulling the data.

try this first if you havent

cheers

 Signature 

magento 1.3.1.2
PHP Version 5.2.6
http://www.getstuff.com.au

 
Magento Community Magento Community
Magento Community
Magento Community
 
davinder
Member
 
Total Posts:  53
Joined:  2008-06-26
 

just updated the list.phtml code which looks like this now

<?php foreach ($this->getStoreCategories() as $_category): ?>
<?php $open 
$this->isCategoryActive($_category); ?>
<?php
$cur_category
=Mage::getModel('catalog/category')->load($_category->getId());
$layer Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
?>     

<div style="float: left; padding-right: 30px; text-align: center;">
                <
div class="linkimage">
                    <
p>
                    <
a href="<?php echo $this->getCategoryUrl($_category)?>">
                    
<?php echo $_category->getName()?>
                    
</a>
                    </
p>
                </
div>
</
div>

<?php endif; ?>
<?php 
endforeach; ?>

and custom design on home page looks like this

<!--<reference name="content">
<
block type="catalog/product_new" name="home.catalog.product.new" alias="product_new" template="catalog/product/new.phtml" after="cms_page"/>
<
block type="reports/product_viewed" name="home.reports.product.viewed" alias="product_viewed" template="reports/home_product_viewed.phtml" after="product_new"/>
<
block type="reports/product_compared" name="home.reports.product.compared" template="reports/home_product_compared.phtml" after="product_viewed" />
</
reference><reference name="right">
<
action method="unsetChild"><alias>right.reports.product.viewed</alias></action>
<
action method="unsetChild"><alias>right.reports.product.compared</alias></action>
</
reference>-->
{{block type="catalog/navigation" name="catalog.category" template="catalog/category/list.phtml"}}

still doesnt show anything.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 8
 
© Copyright 2010 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
177727 users|1136 users currently online|277233 forum posts