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 ) {}
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 ) {} ?>
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?
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?
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?
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??