0

I have written following code to display subcategories

    <ul class="">
  <?php
$_categories=$this->getCurrentChildCategories();
if($_categories->count()):
$categorycount = 0;
foreach ($_categories as $_category):
if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
?>
  <li class="">
    <div class="">
      <div class="">
        <div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="">
          <?php
                    $imageUrl = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/".$this->getCurrentCategory()->getThumbnail();

                    $imageResized = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/"."resize/".$this->getCurrentCategory()->getThumbnail();


                    if (!file_exists($imageResized) && file_exists($imageUrl)) 
                    {

                    $imageObj = new Varien_Image($imageUrl);
                    $imageObj->constrainOnly(TRUE);
                    $imageObj->keepAspectRatio(TRUE);
                    $imageObj->keepFrame(FALSE);
                    $imageObj->quality(100);
                    $imageObj->resize(270, 270);
                    $imageObj->save($imageResized);
                    }
                    ?>
          <span class=""><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/resize/'.$this->getCurrentCategory()->getThumbnail(); ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"/></span> </a> </div>
      </div>
      <div class="">
        <div class="">
          <div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a> </div>
        </div>
      </div>
    </div>
  </li>
  <?php
endif;

endforeach;
endif;
?>
</ul>

I want to add pagination in this page, i have tried solutions like following but it does not worked either

parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
    $pager->setAvailableLimit(array(15=>15));
    $pager->setCollection($_categories);

    $this->setChild('pager', $pager);

Can anyone have any idea on how to add pagination in subcategory listing page?

Please guide me as i am new to magento development.

Thanks in advance.

1 Answer 1

1

Step1: Create IndexController

<?php
class Tbi_Collection_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();    
        $this->renderLayout();
    }
}

Step2: Create Layout xml file

<?xml version="1.0"?>
<layout version="0.1.0">
    <collection_index_index>
        <reference name="content">
            <Tbi type="collection/collection" name="product_list" template="collection/collection.phtml">
            </block>
        </reference>
    </collection_index_index>
</layout>

Step3: Block in the local/block/Collection.php

<?php
class Tbi_Collection_Block_Collection extends Mage_Core_Block_Template
{

    public function __construct()
    {
        parent::__construct();
        $parent_id = Mage::app()->getStore()->getRootCategoryId();
        if($this->getRequest()->getParam('category_id',false)){
            $parent_id = $this->getRequest()->getParam('category_id');
        }
        $collection = Mage::getModel('catalog/category')->getCollection();
        $collection->addFieldToFilter('parent_id',$parent_id);
        $collection->addIsActiveFilter();
        $collection->addNameToResult();
        $collection->addUrlRewriteToResult();
        //$collection->setLoadProductCount(true);
        $this->setCollection($collection);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $parent_id = Mage::app()->getStore()->getRootCategoryId();
        if($this->getRequest()->getParam('category_id',false)){
            $parent_id = $this->getRequest()->getParam('category_id');
        }
        $category = Mage::getModel('catalog/category')->load($parent_id);

        if ($headBlock = $this->getLayout()->getBlock('head')) {
            if ($title = $category->getMetaTitle()) {
                $headBlock->setTitle($title);
            }
            if ($description = $category->getMetaDescription()) {
                $headBlock->setDescription($description);
            }
            if ($keywords = $category->getMetaKeywords()) {
                $headBlock->setKeywords($keywords);
            }
        }
        $this->setTitle($category->getName());


        $toolbar = $this->getToolbarBlock();

        // called prepare sortable parameters
        $collection = $this->getCollection();

        // use sortable parameters
        if ($orders = $this->getAvailableOrders()) {
            $toolbar->setAvailableOrders($orders);
        }
        if ($sort = $this->getSortBy()) {
            $toolbar->setDefaultOrder($sort);
        }
        if ($dir = $this->getDefaultDirection()) {
            $toolbar->setDefaultDirection($dir);
        }
        $toolbar->setCollection($collection);

        $this->setChild('toolbar', $toolbar);
        $this->getCollection()->load();
        return $this;
    }
    public function getDefaultDirection(){
        return 'asc';
    }
    public function getAvailableOrders(){
        return array('name'=> 'Name','position'=>'Position','children_count'=>'Sub Category Count');
    }
    public function getSortBy(){
        return 'name';
    }
    public function getToolbarBlock()
    {
        $block = $this->getLayout()->createBlock('collection/toolbar', microtime());
        return $block;
    }
    public function getMode()
    {
        return $this->getChild('toolbar')->getCurrentMode();
    }

    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }
}

Create the block class ‘collection/toolbar’ which is used in the above Class

<?php
class Tbi_Collection_Block_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar{
    public function getPagerHtml()
    {
        $pagerBlock = $this->getLayout()->createBlock('page/html_pager');

        if ($pagerBlock instanceof Varien_Object) {

            /* @var $pagerBlock Mage_Page_Block_Html_Pager */
            $pagerBlock->setAvailableLimit($this->getAvailableLimit());

            $pagerBlock->setUseContainer(false)
            ->setShowPerPage(false)
            ->setShowAmounts(false)
            ->setLimitVarName($this->getLimitVarName())
            ->setPageVarName($this->getPageVarName())
            ->setLimit($this->getLimit())
            ->setCollection($this->getCollection());
            return $pagerBlock->toHtml();
        }
        return '';
    }
}

Step4: .phtml Files

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
<div class="page-title">
    <h1><?php echo $this->__($this->getTitle()) ?></h1>
</div>
<?php echo $this->getToolbarHtml(); ?>
<?php if($collection->getSize()): ?>

    <?php if($this->getMode()!='grid'): ?>
    <!-- List Model -->
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($collection as $category):
        $category = Mage::getModel('catalog/category')->load($category->getId()); ?>
        <li class="item<?php if( ++$_iterator == sizeof($collection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <a href="<?php echo $category->getUrl() ?>" title="<?php echo $this->stripTags($category->getName()); ?>" class="product-image"><img src="<?php echo $category->getImageUrl() ?>" width="135" height="135" alt="<?php echo $this->stripTags($category->getName()); ?>" /></a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($category->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $category->getUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $category->getName(); ?></a></h2>
                    <div class="desc std">
                        <?php echo $category->getDescription(); ?>
                    </div>
                    <?php if($category->getChildrenCount()){ ?>
                    <div>
                        <a href='<?php echo $this->getUrl('*/*/*',array('category_id'=>$category->getId()))?>'>View Sub Category</a>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
    <?php else: ?>

    <!-- Grid Mode -->

    <?php $_collectionSize = $collection->count() ?>
    <?php
        $_columnCount = $this->getColumnCount();
        if(!$_columnCount){
            $_columnCount = 3;
        }
    ?>
    <?php $i=0; foreach ($collection as $category):
    $category = Mage::getModel('catalog/category')->load($category->getId());
    ?>
        <?php if ($i++%$_columnCount==0): ?>
        <ul class="products-grid">
        <?php endif ?>
            <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
                <a href="<?php echo $category->getUrl() ?>" title="<?php echo $this->stripTags($category->getName()); ?>" class="product-image"><img src="<?php echo $category->getImageUrl() ?>" width="135" height="135" alt="<?php echo $this->stripTags($category->getName()) ?>" /></a>
                <h2 class="product-name"><a href="<?php echo $category->getUrl() ?>" title="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a></h2>
                <?php if($category->getChildrenCount()){ ?>
                <div>
                    <a href='<?php echo $this->getUrl('*/*/*',array('category_id'=>$category->getId()))?>'>View Sub Category</a>
                </div>
                <?php } ?>
            </li>
        <?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
        </ul>
        <?php endif ?>
        <?php endforeach ?>
        <script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
    <?php endif; ?>

<?php echo $this->getToolbarHtml(); ?>
<?php else: ?>
    <p><?php echo $this->__('The collection is empty.'); ?></p>
<?php endif ?>
6
  • Thank You for your reply, Can you please post the path and name of this files (like for indexController, app/code/indexController.php). as i am not able to define its path
    – developer
    Commented Jun 26, 2014 at 9:22
  • Tbi_Collection_IndexController, means Tbi/Colection/IndexController.php, the classes themselves defines the path name, refer to other core files of magento and you will understand Commented Jun 26, 2014 at 9:24
  • Thank You..!! I got it, it will app/code/local/Tbi/Collection/IndexController.php
    – developer
    Commented Jun 26, 2014 at 9:29
  • I have created these files 1. app/code/local/Tbi/Collection/IndexController.php 2. app/code/local/Tbi/Collection/Block/Collection.php 3. app/code/local/Tbi/Collection/Block/Toolbar.php 4. app\design\frontend\default\default\template\collection\collection.phtml 5. app\design\frontend\default\default\layout\collection.xml then created static block with this code {{block type="collection/collection" template="collection/collection.phtml"}} and selected that static block in category, but i'm getting blank page
    – developer
    Commented Jun 26, 2014 at 9:50
  • Please help, did i missed anything?
    – developer
    Commented Jun 26, 2014 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.