|
Hello all,
I spent a bunch of time on this issue and tried about everything I could. What I was trying to find is a way for my subcategories to show up under my main categories in grid view, instead of just displaying all the products. I know a lot of this has been covered, but I just wanted to put out all the code and steps that worked for me. Also, I am running 1.5.0.1 so it should work with any of the latest versions. Hopefully I don’t forget anything as I did all this about 3 days ago. I have copied a lot of this from other forums, but this is not exactly like Avera’s forum post- I am using different code from DirectLowVoltage to get the grid layout. If you want to see an example of all this code working, just visit my categories page:
http://www.itrees.com/species.html and http://www.itrees.com/categories.html
OK, so here goes…
1. Create a static block as follows (props to Magento Fox, Avera, and Brendan.):
Block Title: Sub Category Listing
Identifier: subcategory_listing
Status: Enabled
Content:
{{block type="catalog/navigation" template="catalog/navigation/subcategory_listing.phtml"}}
2. Find the category in question and under the “Display Settings Tab” reflect the following:
Display mode: Static Block Only
CMS Block: Sub Category Listing
Is Anchor: No
THIS IS AN IMPORTANT STEP THAT I THINK SOME PEOPLE FORGET- Under the Custom Design tab you must select the correct custom design template that you are going to be using! This little dumb mistake set me back hours.
3. Create a file called “subcategory_listing.phtml” (without the quotes) and place it here:
app/design/frontend/MY-TEMPLATE-DIR/MY-TEMPLATE-DIR/template/catalog/navigation/
with the following code in it (props to DirectLowVoltage for this):
<?php $_categories=$this->getCurrentChildCategories(); ?> <br>
<div class="category-products"> <?php $_collectionSize = $_categories->count() ?> <table class="products-grid" id="products-grid-table"> <?php $i=0; foreach ($_categories as $_category): ?> <?php if ($i++%3==0): ?> <tr> <?php endif ?> <td> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"> <img src="<?php echo $_category->getImageUrl() ?>" width="180" alt="<?php echo $this->htmlEscape($_category->getName()) ?>" /> </a> <h3 class="product-name"><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"> <?php echo $this->htmlEscape($_category->getName()) ?></a></h3> </td> <?php if ($i%3==0 && $i!=$_collectionSize): ?> </tr> <?php endif ?> <?php endforeach ?> <?php for($i;$i%3!=0;$i++): ?> <td class="empty"> </td> <?php endfor ?> <?php if ($i%3==0): ?> </tr> <?php endif ?> </table> </div> <br>
I’ll state the obvious as well, you must assign a picture to your categories. If you want to change the size of the pictures, simply edit the width attribute in the above code. You can also create css for displaying boxes around everything and formatting the text, this is all done in the boxes.css file. I didn’t really mess with that because I thought mine looked fine without. Someone else will have to figure that out, because I am really not that good at programming- only good at scouring forums and copying off of people.
4. Copy:
app\code\core\Mage\Catalog\Block\Navigation.php
to
app\code\local\Mage\Catalog\Block\Navigation.php
(we are making a local copy so that future Magento upgrades don’t overwrite our work today.)
Then open the local version of “Navigation.php”, find:
public function getCurrentChildCategories() { $layer = Mage::getSingleton('catalog/layer'); $category = $layer->getCurrentCategory(); /* @var $category Mage_Catalog_Model_Category */ $categories = $category->getChildrenCategories(); $productCollection = Mage::getResourceModel('catalog/product_collection'); $layer->prepareProductCollection($productCollection); $productCollection->addCountToCategories($categories); return $categories; }
And replace it with:
public function getCurrentChildCategories() { $layer = Mage::getSingleton('catalog/layer'); $category = $layer->getCurrentCategory(); /* @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') ->addAttributeToSelect('image') ->addAttributeToFilter('is_active', 1) ->addIdFilter($category->getChildren()) ->setOrder('position', 'ASC') ->joinUrlRewrite() ->load();
$productCollection = Mage::getResourceModel('catalog/product_collection'); $layer->prepareProductCollection($productCollection); $productCollection->addCountToCategories($collection); return $collection; }
5. Edit your css to make it look good in the boxes.css file.
|