Tuesday, March 8, 2016

Display Category Collection With Toolbar in Magento

Magento provide product listing with Grid view and List view,Order by ascending and descending with pagination functionality which is best and highly designed website functionality but some times you want to implement that toolbar functionality for category collection. For some Magento story design you want to implement sub category listing same as products listing than you must modify Magento core functionality because it not provide this type of functionality.
See below process for create own extension in local pool for display sub category listing with toolbar.
Create new `Newsinfo_Subcategories.xml` file in `app\etc\modules\` as below code.

<?xml version="1.0"?>
<config>
    <modules>
        <Newsinfo_Subcategories>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Newsinfo_Subcategories>
    </modules>
</config>
 
Create new `config.xml` file in `app\code\local\Newsinfo\Subcategories\etc\`as below code.

<?xml version="1.0"?>
<config>
    <modules>
        <Newsinfo_Subcategories>
            <version>1.0.0</version>
        </Newsinfo_Subcategories>
    </modules>
    <global>
        <blocks>
            <grbrains>
                <class>Newsinfo_Subcategories_Block</class>
            </grbrains>
        </blocks>
        <helpers>
            <grbrains>
                <class>Newsinfo_Subcategories_Helper</class>
            </grbrains>
        </helpers>
    </global>
</config> 

create new `Data.php` file in`app\code\local\Newsinfo\Subcategories\Helper\` with class as below.


class Newsinfo_Subcategories_Helper_Data extends Mage_Core_Helper_Abstract
{
   //Enter helper data functions.
 }
create new `Subcategories.php` file in `app\code\local\Newsinfo\Subcategories\Block\`that display subcategories on fronted as below code.

class Newsinfo_Subcategories_Block_Subcategories extends Mage_Core_Block_Template
{
    public function __construct()
    {
        parent::__construct();
        $category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
        $collection = Mage::getModel('catalog/category')->getCollection();
        $collection->addFieldToFilter('parent_id',$category->getId());
        $collection->addIsActiveFilter();
        $collection->addNameToResult();
        $collection->addUrlRewriteToResult();
        $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);
            }
        } 
        $toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
  $pager = $this->getLayout()->createBlock('page/html_pager', microtime());
        $toolbar->setChild('product_list_toolbar_pager', $pager);
        $collection = $this->getCollection();
        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 'position';
    }
    public function getMode()
    {
       return $this->getChild('toolbar')->getCurrentMode();
    }
    public function getToolbarHtml()
    {
        return $this->getChildHtml('toolbar');
    }
 public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
}

Now it's ready for call in website here display all sub category listing than first create new static block from Admin->CMS->Static Blocks->Add New Block with below details.

Block Title : Sub Categories
Identifier : subcategories
Status : Enable
Content :{{block type="newsinfo/subcategories" template="catalog/category/subcategory.phtml"}}

Save below block and  set block permission from Admin->System->Permission->Blocks and create new block with name as`newsinfo/subcategories` and status as `Allowed`.
Now call created static block on any category from Admin->catalog->Manage Categories->CATEGORY->Display Settings set `Display Mode` as `Static block only` and `CMS Block` as  'Sub Categories' (created as below).
Now create `subcategory.phtml` file in `app\design\frontend\THEME\default\template\catalog\category\` as below code.

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<?php $collection = $this->getCollection(); ?>
<?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; ?>"> <a href="<?php echo $category->getUrl() ?>" title="<?php echo $this->stripTags($category->getName()); ?>" class="product-image"><img src="<?php echo $category->getImageUrl();?>" alt="<?php echo $this->stripTags($category->getName()) ?>" /></a>
      <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>
        </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>
    <?php endif;?>
    <li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
      <div class="product-bdr-area">
        <div class="product-img-area"> <a href="<?php echo $category->getUrl() ?>" title="<?php echo $this->stripTags($category->getName()); ?>" class="product-image"><img src="<?php echo $category->getImageUrl()?>" alt="<?php echo $this->stripTags($category->getName()) ?>" /></a> <a href="<?php echo $category->getUrl() ?>" title="<?php echo $category->getUrl() ?>"><?php echo $category->getName() ?></a> </div>
      </div>
    </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 else: ?>
<p class="note-msg"><?php echo $this->__('There is no sub category'); ?></p>
<?php endif ?>

No comments :

Post a Comment