Snippets with the tag: category

Current category name

Description

Get the current category name

Code

Mage::registry('current_category')->getName();

Display new products from specific category on home page

Description

code to display new products from a particular category on home page

Code

{{block type="catalog/product_list" category_id="10" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}

Get categories products ordered by number of sales

Description

Get categories products ordered by number of sales

Code

$categoryId = 1; 
$category = Mage::getModel('catalog/category')->load($categoryId); 
$products = Mage::getResourceModel('reports/product_collection') 
          ->addOrderedQty() 
          ->addAttributeToSelect('*') 
          ->setOrder('ordered_qty', 'desc') 
          ->addCategoryFilter($category);

Get all top level categories

Description

Get all of the top level categories. Usually, the first base category added will have the id of 2.

Code

$defaultCategoryId = 2; 
$topLevelCats = Mage::getModel('catalog/category') 
->getCollection() 
->addAttributeToSelect('path','name','id') 
->addAttributeToFilter('parent_id', $defaultCategoryId) 
->setOrder('position', 'ASC');

Check if RSS is enabled

Description

Check if RSS is enabled

Code

Mage::getStoreConfig('rss/catalog/category');

Category navigation listings

Description

Make sure the block that you’re working on is of the type catalog/navigation. If you’re editing catalog/navigation/left.phtml it should be okay.

Code

<div id="leftnav">
    <?php $helper = $this->helper('catalog/category') ?>
    <?php $categories = $this->getStoreCategories() ?>
    <?php if (count($categories) > 0): ?>
        <ul id="leftnav-tree" class="level0">
            <?php foreach($categories as $category): ?>
                <li class="level0<?php if ($this->isCategoryActive($category)): ?> active<?php endif; ?>">
                    <a href="<?php echo $helper->getCategoryUrl($category) ?>"><span><?php echo $this->escapeHtml($category->getName()) ?></span></a>
                    <?php if ($this->isCategoryActive($category)): ?>
                        <?php $subcategories = $category->getChildren() ?>
                        <?php if (count($subcategories) > 0): ?>
                            <ul id="leftnav-tree-<?= $category->getId(); ?>" class="level1">
                                <?php foreach($subcategories as $subcategory): ?>
                                    <li class="level1<?php if ($this->isCategoryActive($subcategory)): ?> active<?php endif; ?>">
                                        <a href="<?= $helper->getCategoryUrl($subcategory); ?>"><?= $this->escapeHtml(trim($subcategory->getName(), '- ')); ?></a>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                            <script type="text/javascript">decorateList('leftnav-tree-<?php echo $category->getId() ?>', 'recursive')</script>
                        <?php endif; ?>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
        <script type="text/javascript">decorateList('leftnav-tree', 'recursive')</script>
    <?php endif; ?>
</div>

Load products by category ID

Description

Load products by category ID, for example 12.

Code

$_category = Mage::getModel('catalog/category')->load(12);
$_productCollection = $_category->getProductCollection();
if($_productCollection->count()) {
    foreach( $_productCollection as $_product ):
        echo $_product->getProductUrl();
        echo $this->getPriceHtml($_product, true);
        echo $this->htmlEscape($_product->getName());
    endforeach;
}