I have a Magento shop and want to display the top-categories with description and image on the frontpage. Does anyone know how i can fix this?
I have this to display the top-categories:
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
<?php echo $_category->getName() ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
But i can't get it showing also the description and image. Hope someone can help me with this.
Regards, Robert
You can achieve this using module:
Note: In this approach, you can display the block where you want to display(Home page, footer, sidebar, other pages).
Steps: Here, [Namespace]=Codilar & [ModuleName]=Category
Create xml : app/etc/modules/Codilar_Category.xml
<?xml version="1.0"?>
<config>
<modules>
<Codilar_Category>
<active>true</active>
<codePool>local</codePool>
</Codilar_Category>
</modules>
</config>
Create config.xml: app/code/local/Codilar/Category/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Codilar_Category>
<version>1.0</version>
</Codilar_Category>
</modules>
<global>
<blocks>
<codilar_category>
<class>Codilar_Category_Block</class>
</codilar_category>
</blocks>
<models>
<codilar_category>
<class>Codilar_Category_Model</class>
</codilar_category>
</models>
</global>
</config>
Create Model: app/code/local/Codilar/Category/Model/Category.php
<?php
class Codilar_Category_Model_Category extends Mage_Core_Model_Abstract {
public function getActiveCategory() {
$categories = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter();
return $categories;
}
}
Create Block: app/code/local/Codilar/Category/Block/Category.php
<?php
class Codilar_Category_Block_Category extends Mage_Core_Block_Template {
public function getActiveCategory() {
$arr_categories = array();
$categories = Mage::getModel("codilar_category/category")->getActiveCategory();
foreach ($categories as $category) {
$arr_categories[] = array(
'id' => $category->getId(),
'name' => $category->getName(),
'url' => $category->getProductUrl(),
'image' => $category->getThumbnail(),
'desc' => $category->getDescription(),
);
}
return $arr_categories;
}
}
Create template file: app/design/frontend/default/default/template/codilar/category/category.phtml
<?php
$categories = $this->getActiveCategory();
?>
<div id="category_list">
<h1>All Category</h1>
<?php if (is_array($categories) && count($categories)) { ?>
<?php foreach($categories as $category) { ?>
<div>
<a href="<?php echo $category['url'] ?>"><?php echo $category['name'] ?></a>
</div>
<div>
<img src="<?php echo Mage::getBaseUrl('media').'catalog/category/'. $category['image']; ?>" alt="<?php echo $category['image'] ?>" height="100" width="100">
</div>
<div>
<p><?php echo $category['desc'] ?></p>
</div>
<?php } ?>
<?php } ?>
</div>
To insert your category custom block using the layout update file, you can use the following code.
<block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"></block>
On the other hand, if you want to display your category custom block using the CMS page, here is the code you should use.
{{block type="codilar_category/category" name="codilar_category_category" template="codilar/category/category.phtml"}}
I have it working to show 2 products from each top level category with te following code:
$count = 0;
$_productCollection = Mage::getModel('catalog/category')->load($_categoryId)
->getProductCollection()
->addAttributeToSelect('*')
->setOrder('date', 'ASC');
?>
<div class="home-featured-product">
<?php foreach ($_productCollection as $_product): ?>
<li class="item">
<div class="regular">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(270, 200); ?>" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</a>
</div>
<a class="product-name" href="<?php echo $_product->getProductUrl() ?>">
<?php echo $this->htmlEscape($_product->getName()) ?>
</a>
<?php $productObject = Mage::getModel('catalog/product')->load($_product->getId());?>
<?php echo $this->getPriceHtml($productObject, true) ?>
</li>
<?php
if($count == 1) break; // Stop after 2 items
$count++;
?>
<?php endforeach ?>
</div>
But i also need to show the description and price but somehow the getPriceHtml doesn't seem to work :(