2

How to include toolbar.phtml in my custom module

How to call toolbar in my custom Module

Feature.php

 <?php 

class MPS_AllProduct_Block_Categoryid_List extends Mage_Catalog_Block_Product_Abstract
{   

    /**
    * Get Feature Product 
    **/
    public function _getProductCollection(){
       $category = Mage::getModel('catalog/category')->load(3);
        $_productCollection = Mage::getModel('catalog/product')
            ->getCollection()
            ->addAttributeToSelect('*')
            ->addCategoryFilter($category)
            ->addAttributeToFilter('status', array('eq' => 1))
            ->addAttributeToFilter('visibility', 4)
            ->addFieldToFilter(array(array('attribute'=>'feature','eq'=>'1'),))
            ->addAttributeToSort($_GET["order"], $_GET["dir"])
            ->joinField('is_in_stock',
                'cataloginventory/stock_item',
                'is_in_stock',
                'product_id=entity_id',
                'is_in_stock=1',
                '{{table}}.stock_id=1',
                'left');
            return $_productCollection;

                $itemsLimit =$_GET["limit"] ? $_GET["limit"] : Mage::getStoreConfig('catalog/frontend/grid_per_page');   //Set items to show per page
                $currPage=$_GET["p"] ? $_GET["p"] : 1;                //Set current page      
               /*   Set Pagination for Custom Loaded Collection */                              
                $toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
                $toolbar->setCollection($_productCollection);

                /*  Set Pager   */
                $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
                $pager->setAvailableLimit(array($itemsLimit=>$itemsLimit));
                $pager->setCollection($_productCollection);
                $toolbar->setChild('product_list_toolbar_pager', $pager);
                $toolbar->setData('_current_limit', $itemsLimit);


    }


}

feature.phtml

 <?php
    $_productCollection=$this->_getProductCollection();
    $_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">

    <?php echo $this->getToolbarHtml() ?>

    <?php // List mode ?>
    <?php if($this->getMode()!='grid'): ?>
    <?php $_iterator = 0; ?>
    <ol class="products-list" id="products-list">
    <?php foreach ($_productCollection as $_product): ?>
        <li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
            <?php // Product Image ?>
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
            <?php // Product description ?>
            <div class="product-shop">
                <div class="f-fix">
                    <?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
                    <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
                    <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product) ?>
                    <?php endif; ?>
                    <?php echo $this->getPriceHtml($_product, true) ?>
                    <?php if($_product->isSaleable()): ?>
                        <p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <div class="desc std">
                        <?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
                        <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>" class="link-learn"><?php echo $this->__('Learn More') ?></a>
                    </div>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </div>
            </div>
        </li>
    <?php endforeach; ?>
    </ol>
    <script type="text/javascript">decorateList('products-list', 'none-recursive')</script>

    <?php else: ?>

    <?php // Grid Mode ?>

    <?php $_collectionSize = $_productCollection->count() ?>
    <?php $_columnCount = $this->getColumnCount(); ?>
    <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?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 $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
                <?php if($_product->getRatingSummary()): ?>
                <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <div class="actions">
                    <?php if($_product->isSaleable()): ?>
                        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
                    <?php else: ?>
                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
                    <?php endif; ?>
                    <ul class="add-to-links">
                        <?php if ($this->helper('wishlist')->isAllow()) : ?>
                            <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>
                        <?php endif; ?>
                        <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>
                        <?php endif; ?>
                    </ul>
                </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; ?>

    <div class="toolbar-bottom">

        <?php echo $this->getToolbarHtml() ?>
    </div>
</div>
<?php endif; ?>

It's not display Toolbar

8
  • Do you mean 'catalog/product/list/toolbar.phtml' template?
    – WaPoNe
    Commented Aug 17, 2016 at 14:08
  • Yes i call that but i got error. is there any other way or tell me how to call
    – Magento 2
    Commented Aug 17, 2016 at 16:12
  • try $this->setCollection($this->FeatureProduct()); just before calling tollbar or set this in constructor. Commented Aug 22, 2016 at 12:36
  • No still same error
    – Magento 2
    Commented Aug 22, 2016 at 12:40
  • Have you try my solution? Commented Aug 23, 2016 at 11:43

3 Answers 3

3
+50

I have created toolbar With my custom below code

In my list.phtml

<?php echo $this->getToolbarHtml(); ?>// Default way
<?php  
       // Custom Way
    $toolbar = $this->getToolbarBlock();
    //$toolbar->setCollection($this->getChild('catalog_product_list')->getLoadedProductCollection());
    $toolbar->setCollection($_productCollection);
    $layout = Mage::getSingleton('core/layout');
    $pager = $layout->createBlock('page/html_pager');
    $toolbar->setChild('product_list_toolbar_pager', $pager);
    echo $toolbar->toHtml();

?>

and your Block class Should extend Like below class

class Mypackge_Mymodule_Block_Listing extends Mage_Catalog_Block_Product_List 

This Is how I solve this Problem

and put filter in My collection On the bases of $_GET Values

4
  • Fatal error: Call to a member function setCollection() on a non-object in
    – Magento 2
    Commented Aug 23, 2016 at 11:45
  • Have you extend the same class that I have suggested Commented Aug 23, 2016 at 11:48
  • Yes Check my updated question i added full function
    – Magento 2
    Commented Aug 23, 2016 at 11:53
  • extend this class Mage_Catalog_Block_Product_List try and let me know Commented Aug 23, 2016 at 12:04
1

Use below snippet to add pagination and sorting on your custom collection. For every custom collection listing you have to create custom toolbar pager as well. Add below code after loading your collection.

    $itemsLimit         =   $_GET["limit"] ? $_GET["limit"] : Mage::getStoreConfig('catalog/frontend/grid_per_page');   //Set items to show per page
    $currPage               =   $_GET["p"] ? $_GET["p"] : 1;                //Set current page      
   /*   Set Pagination for Custom Loaded Collection */                              
    $toolbar = Mage::getBlockSingleton('catalog/product_list')->getToolbarBlock();
    $toolbar->setCollection($_productCollection);

    /*  Set Pager   */
    $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
    $pager->setAvailableLimit(array($itemsLimit=>$itemsLimit));
    $pager->setCollection($_productCollection);
    $toolbar->setChild('product_list_toolbar_pager', $pager);
    $toolbar->setData('_current_limit', $itemsLimit);

After this, replace

$this->getToolbarHtml(); by $toolbar->toHtml(); 

to display bottom pager and top sorting toolbar.

For sorting order, do this before collection load:

$_productCollection->addAttributeToSort($_GET["order"], $_GET["dir"]');

I hope this resolves your problem. Please refer to catalog/product/list.phtml to clearly understand my answer.

Note: Block type pf view file (.phtml) should be catalog/product_list

3
  • NO it didn't work check my updated question
    – Magento 2
    Commented Aug 23, 2016 at 11:42
  • have you replaced $this->getToolbarHtml(); by $toolbar->toHtml(); ? Commented Aug 23, 2016 at 14:30
  • What is the error now ? any change ? Commented Aug 24, 2016 at 5:30
1

You can try with following way.

test.php

class Namespace_Module_Block_test extends Mage_Catalog_Block_Product_List
{

    public function __construct()
    {

    }

    function get_prod_count()
    {
       //unset any saved limits
       Mage::getSingleton('catalog/session')->unsLimitPage();
      return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 9;
    }

    function get_cur_page()
    {
      return (isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 1;
    }

    protected function _getProductCollection()
    {
        parent::__construct();


$storeId    = Mage::app()->getStore()->getId();
                $collection = Mage::getResourceModel('reports/product_collection')
                            ->addAttributeToSelect('*');
                $totalDay = 60;
                $currentDate  = Mage::app()->getLocale()->date();        



            $startDate = strtotime ( '-'.$totalDay.' day' , strtotime ( $currentDate ) ) ;
            $startDate = date ( 'Y-m-j' , $startDate );

            $currentDate = date ( 'Y-m-j' , strtotime($currentDate) );         

            $collection->addFieldToFilter('created_at',array('from'=>$startDate,'to'=>$currentDate))
                     ->addOrderedQty()
                     ->setStoreId($storeId)
                     ->addStoreFilter($storeId)
                     ->setOrder('ordered_qty', 'desc')
                     ->setPageSize($this->get_prod_count())
                     ->setCurPage($this->get_cur_page());

            $collection = $this->_addProductAttributesAndPrices($collection);

            $productFlatData = Mage::getStoreConfig('catalog/frontend/flat_catalog_product');

            if($productFlatData == "1")
            {
                 $collection->getSelect()
                    ->joinLeft(
                        array('cpl' => $collection->getResource()->getFlatTableName()),
                            "e.entity_id = cpl.entity_id"
                    )
                    ->where("cpl.visibility IN (?)", 
                        array(
                            Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG, 
                            Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH
                        )
                    );
            }

            Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
            Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
            Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);

            $this->_productCollection = $collection;
        return $this->_productCollection;
    }

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

        $pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
        $pager->setCollection($this->_getProductCollection());
        $this->setChild('pager', $pager);
        $this->_getProductCollection()->load();
        return $this;
    }

    public function getPagerHtml()
    {
        return $this->getChildHtml('pager');
    }
}

This is custom collection so don't used it.

And test.phtml

used

<?php echo $this->getToolbarHtml(); ?>

so you can get custom toolbar in your custom collection.

hope this help of you.

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.