Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Need Toolbar on New Product Page
 
Nimesh Patel
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2008-05-09
Ahmedabad, India
 

Hello Everybody,

i had made one cms page that shows new products from catalog/product/new.phtml

I want to show toolbar in that product listing.

i had put that code from product/list.phtml

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

But i will not show tollbar on new products page.

Can anyone guide me how can i show toolbar on product listing page

Please help

 
Magento Community Magento Community
Magento Community
Magento Community
 
Nimesh Patel
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2008-05-09
Ahmedabad, India
 

I got the solution

public function getToolbarBlock()
    
{
        
if ($blockName $this->getToolbarBlockName()) {
            
if ($block $this->getLayout()->getBlock($blockName)) {
                
return $block;
            
}
        }
        $block 
$this->getLayout()->createBlock($this->_defaultToolbarBlockmicrotime());
        return 
$block;
    
}
    
public function setCollection($collection)
    
{
        $this
->_productCollection $collection;
        return 
$this;
    
}

ON ode\core\Mage\Catalog\Block\Product\New.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
Nimesh Patel
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2008-05-09
Ahmedabad, India
 

also add thet code on product/new.phtml

<?php 
$toolbar 
$this->getToolbarBlock();
$toolbar->setCollection($this->getProductCollection());
echo 
$toolbar->toHtml();
?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
dabjoern
Jr. Member
 
Avatar
Total Posts:  25
Joined:  2009-07-01
 

Hi there,

I’ve applied your code but right now it’s not working as expected. The toolbar is displayed and the counter is working and the pagination as well. But as soon as you go to page 2 the same products are displayed and the counter is behaving strangly. Any ideas? Here’s my code:

class MyLib_Block_Product_New extends Mage_Catalog_Block_Product_New
{
    
protected $_defaultToolbarBlock 'catalog/product_list_toolbar';
    
    protected 
$_productsCount null;

    const 
DEFAULT_PRODUCTS_COUNT 20;

    protected function 
_beforeToHtml()
    
{
        $todayDate  
Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        
        
$collection Mage::getResourceModel('catalog/product_collection');
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        
        
$collection $this->_addProductAttributesAndPrices($collection)
            ->
addStoreFilter()
            ->
addAttributeToFilter('news_from_date', array('date' => true'to' => $todayDate))
            ->
addAttributeToFilter('news_to_date', array('or'=> array(
                
=> array('date' => true'from' => $todayDate),
                
=> array('is' => new Zend_Db_Expr('null')))
            ), 
'left')
            ->
addAttributeToSort('news_from_date''desc')
            ->
setPageSize($this->getProductsCount())
            ->
setCurPage(1)
        ;
        
$this->setProductCollection($collection);

        return 
parent::_beforeToHtml();
    
}

    
public function setProductsCount($count)
    
{
        $this
->_productsCount $count;
        return 
$this;
    
}

    
public function getProductsCount()
    
{
        
if (null === $this->_productsCount{
            $this
->_productsCount self::DEFAULT_PRODUCTS_COUNT;
        
}
        
return $this->_productsCount;
    
}

    
public function getToolbarBlock()
    
{
        
if ($blockName $this->getToolbarBlockName()) {
        
echo $this->getLayout()->getBlock($blockName);
            if (
$block $this->getLayout()->getBlock($blockName)) {
                
return $block;
            
}
        }
        $block 
$this->getLayout()->createBlock($this->_defaultToolbarBlockmicrotime());
        return 
$block;
    
}

    
public function setCollection($collection)
    
{
        $this
->_productCollection $collection;
        return 
$this;
    
}
}

Thanks, dabjoern

P.S. All products (24 instead of 20) are listed.

 
Magento Community Magento Community
Magento Community
Magento Community
 
MarkStall
Jr. Member
 
Total Posts:  6
Joined:  2009-10-22
 

Any luck with the solution ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sepp666
Jr. Member
 
Total Posts:  2
Joined:  2009-09-24
 

I get this error message after following the first two instructions

Fatal error: Call to a member function setCollection() on a non-object

Does anybody know a solution? Toolbar would be perfect for the “new” - feature! Cheers!

 
Magento Community Magento Community
Magento Community
Magento Community
 
optick
Member
 
Avatar
Total Posts:  34
Joined:  2008-07-20
Brasov, Romania
 

New.php

<?php
class Mage_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_Abstract
{
    
protected $_defaultToolbarBlock 'catalog/product_list_toolbar';
    
    protected 
$_productsCount null;
    
    protected 
$_defaultColumnCount 3;

    const 
DEFAULT_PRODUCTS_COUNT 9;

    protected function 
_beforeToHtml()
    
{
        $toolbar 
$this->getToolbarBlock();
        
        
$todayDate  Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        
        
$collection Mage::getResourceModel('catalog/product_collection');
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        
        
$collection $this->_addProductAttributesAndPrices($collection)
            ->
addStoreFilter()
            ->
addAttributeToFilter('news_from_date', array('date' => true'to' => $todayDate))
            ->
addAttributeToFilter('news_to_date', array('or'=> array(
                
=> array('date' => true'from' => $todayDate),
                
=> array('is' => new Zend_Db_Expr('null')))
            ), 
'left')
            ->
addAttributeToSort('news_from_date''desc')
        ;
        
        
// use sortable parameters
        
if ($orders $this->getAvailableOrders()) {
            $toolbar
->setAvailableOrders($orders);
        
}
        
if ($sort $this->getSortBy()) {
            $toolbar
->setDefaultOrder($sort);
        
}

        $this
->setProductCollection($collection);
        
        
$toolbar->setCollection($collection);

        
$this->setChild('toolbar'$toolbar);
        
Mage::dispatchEvent('catalog_block_product_list_collection', array(
            
'collection'=>$collection,
        ));
        
        
$collection->load();
        
Mage::getModel('review/review')->appendSummary($collection);
        
        return 
parent::_beforeToHtml();
    
}
    
    
public function getToolbarBlock()
    
{
        
if ($blockName $this->getToolbarBlockName()) {
            
if ($block $this->getLayout()->getBlock($blockName)) {
                
return $block;
            
}
        }
        $block 
$this->getLayout()->createBlock($this->_defaultToolbarBlockmicrotime());
        return 
$block;
    
}
    
    
public function setCollection($collection)
    
{
        $this
->_productCollection $collection;
        return 
$this;
    

}
?>

new.phtml

<?php 
$toolbar 
$this->getToolbarBlock();
$toolbar->setCollection($this->getProductCollection());
echo 
$toolbar->toHtml();
?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
BaluErtl
Jr. Member
 
Total Posts:  11
Joined:  2009-09-03
 

@optick:

I’ve pasted and overrided both of these files, and toolbar appears correctly, but it’s functionality is not okay, my problem is quite similar to dabjoern’s. Product counting works, but sort order and limiting does not.

I compared New.php and List.php which are similar and extends the same object class, so I guess solution might be somewhere on of their function call. Does anyone have a closer clue how to use list.phtml instead new.phtml?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Jerry Guo
Sr. Member
 
Total Posts:  85
Joined:  2008-08-18
china
 

I rewrite New.php in app/Catalog/Block/Product/

class Mage_Catalog_Block_Product_List_New extends Mage_Catalog_Block_Product_List
{
    
protected $_productsCount null;

    const 
DEFAULT_PRODUCTS_COUNT 5;

    protected function 
_getProductCollection()
    
{
        $todayDate  
Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
        
        
$collection Mage::getResourceModel('catalog/product_collection');
        
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
        
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
        
        
$collection $this->_addProductAttributesAndPrices($collection)
            ->
addStoreFilter()
            ->
addAttributeToFilter('news_from_date', array('date' => true'to' => $todayDate))
            ->
addAttributeToFilter('news_to_date', array('or'=> array(
                
=> array('date' => true'from' => $todayDate),
                
=> array('is' => new Zend_Db_Expr('null')))
            ), 
'left')
            ->
addAttributeToSort('news_from_date''desc')
            ->
setPageSize($this->getProductsCount())
            ->
setCurPage(1)
        ;
        
$this->setProductCollection($collection);

       return 
$collection;   }
copy it to app/Catalog/Block/Product/List

in the frontend you can use list.phtml

I’m lucky , solved it

File Attachments
New.php  (File Size: 3KB - Downloads: 389)
 
Magento Community Magento Community
Magento Community
Magento Community
 
smivks
Member
 
Avatar
Total Posts:  45
Joined:  2009-05-02
 

Hi,

I need same paging toolbar for different purpose, not for product listing.

I developed custom module and want to integrate toolbar with it.

So i can able to integrate toolbar with it, just 1 problem is that when i navigate to second page, result is same.
First page and Second page result is same.

Is there some type of filter which i have to use?

My collection class contains following code

class Myown_QA_Model_Mysql4_QA_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    
public function _construct()
    
{
        parent
::_construct();
        
$this->_init('qa/qa');
    
}
    
    
public function addEntityFilter($entityId)
    
{    
        $this
->getSelect()->where('entity_id = (?)'$entityId);
        return 
$this;
    
}
    
    
public function addStatusFilter($status)
    
{
       $this
->getSelect()->where('status = (?)'$status);
        return 
$this;
    
}
}

And in my block file i am creating pager like this.

class Myown_QA_Block_Index extends Mage_Core_Block_Template{
    
protected $_qaCollection;

    public function 
_prepareLayout(){
        parent
::_prepareLayout();        
        
$pager $this->getLayout()->createBlock('page/html_pager''qa.index.pager')
            ->
setCollection($this->getQACollection());
        
$this->setChild('pager'$pager);        
        return 
$this;
    
}

    
public function getQACollection(){
        $collection 
Mage::getModel('qa/qa')->getCollection()
                        ->
addEntityFilter(Mage::app()->getRequest()->getParam('id'false))
                        ->
addStatusFilter("Approved");
        return 
$this->_qaCollection;
    
}
}

Any thing else if some one need from myside to solve this problem.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
smivks
Member
 
Avatar
Total Posts:  45
Joined:  2009-05-02
 

No one know solution?

 
Magento Community Magento Community
Magento Community
Magento Community
 
smivks
Member
 
Avatar
Total Posts:  45
Joined:  2009-05-02
 

I have solved this issue, by adding some line of code in my Collection.php file and in block file in which i am using Collection.php functions to get collection.

I do not know this is best solution or not, but for now it works for me.

So now my block file contains following code.

class Myown_QA_Block_Index extends Mage_Core_Block_Template{
    
protected $_qaCollection;

    public function 
_prepareLayout(){
        parent
::_prepareLayout();        
        
$pager $this->getLayout()->createBlock('page/html_pager''qa.index.pager')
            ->
setCollection($this->getQACollection());
        
$this->setChild('pager'$pager);        
        return 
$this;
    
}

    
public function getQACollection(){
         $collection 
Mage::getModel('qa/qa')->getCollection()
                        ->
addEntityFilter(Mage::app()->getRequest()->getParam('id'false))
                        ->
addStatusFilter("Approved");
                
$limit 10;
        
$offset 0;
        
        if(
$this->getRequest()->getParam('limit')){
            $limit 
$this->getRequest()->getParam('limit');
        
}
        
        
if($this->getRequest()->getParam('p')){
            $offset 
= ($this->getRequest()->getParam('p') * $limit) - $limit;
        
}
        
        $collection
->addLimitFilter($limit$offset);
        
$this->_qaCollection $collection;
        return 
$this->_qaCollection;
    
}
}

And my Collection.php contains following code

class Myown_QA_Model_Mysql4_QA_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    
public function _construct()
    
{
        parent
::_construct();
        
$this->_init('qa/qa');
    
}
    
    
public function addEntityFilter($entityId)
    
{    
        $this
->getSelect()->where('entity_id = (?)'$entityId);
        return 
$this;
    
}
    
    
public function addStatusFilter($status)
    
{
       $this
->getSelect()->where('status = (?)'$status);
        return 
$this;
    
}
    
public function addLimitFilter($limit$offset){
        $this
->getSelect()->limit($limit$offset);
        return 
$this;
    
}
}

That’s All

 
Magento Community Magento Community
Magento Community
Magento Community
 
smivks
Member
 
Avatar
Total Posts:  45
Joined:  2009-05-02
 

I solved this issue, by adding some line of code in my Collection.php file and in block file in which i am using Collection.php functions to get collection.

I do not know this is best solution or not, but for now it works for me.

So now my block file contains following code.

class Myown_QA_Block_Index extends Mage_Core_Block_Template{
    
protected $_qaCollection;

    public function 
_prepareLayout(){
        parent
::_prepareLayout();        
        
$pager $this->getLayout()->createBlock('page/html_pager''qa.index.pager')
            ->
setCollection($this->getQACollection());
        
$this->setChild('pager'$pager);        
        return 
$this;
    
}

    
public function getQACollection(){
         $collection 
Mage::getModel('qa/qa')->getCollection()
                        ->
addEntityFilter(Mage::app()->getRequest()->getParam('id'false))
                        ->
addStatusFilter("Approved");
                
$limit 10;
        
$offset 0;
        
        if(
$this->getRequest()->getParam('limit')){
            $limit 
$this->getRequest()->getParam('limit');
        
}
        
        
if($this->getRequest()->getParam('p')){
            $offset 
= ($this->getRequest()->getParam('p') * $limit) - $limit;
        
}
        
        $collection
->addLimitFilter($limit$offset);
        
$this->_qaCollection $collection;
        return 
$this->_qaCollection;
    
}
}

And my Collection.php contains following code

class Myown_QA_Model_Mysql4_QA_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    
public function _construct()
    
{
        parent
::_construct();
        
$this->_init('qa/qa');
    
}
    
    
public function addEntityFilter($entityId)
    
{    
        $this
->getSelect()->where('entity_id = (?)'$entityId);
        return 
$this;
    
}
    
    
public function addStatusFilter($status)
    
{
       $this
->getSelect()->where('status = (?)'$status);
        return 
$this;
    
}
    
public function addLimitFilter($limit$offset){
        $this
->getSelect()->limit($limit$offset);
        return 
$this;
    
}
}

That’s All

 
Magento Community Magento Community
Magento Community
Magento Community
 
rosew
Member
 
Total Posts:  43
Joined:  2009-03-07
Leatherhead UK
 

I had the same problem, but managed to get it to work in the end.
Where I went wrong (and I guess the rest of you) is by not getting my collection that’s output by the record template limited by the pager. I effectively passed my collection onto the pager, set the limit in there, but then used a fresh full collection, instead of the correct limited collection, in my template outputting the records.

If it helps anyone, here’s the applicable code from my block:
public function __construct()
{
parent::__construct();
$this->setTemplate(’customer/balance.phtml’);
$customerId = $this->getCustomer()->getId();
$transactions = Mage::getModel(’customerext/transaction’)
->getCollection()
->addFieldToFilter(’customer_id’,$customerId)
->addOrder(’created’,’desc’);
$this->setTransactions($transactions);
}

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

$pager = $this->getLayout()->createBlock(’page/html_pager’, ‘customer.balance.pager’)
->setCollection($this->getTransactions());
$this->setChild(’pager’, $pager);
$this->getTransactions()->load();
return $this;
}

Looking at smivks original code that had the same issue, mine looked the same - I also had a separate function to set my Collection (Transactions in my case). Moving this code to the block constructor fixed it.

Hope that helps someone.

Rose

 
Magento Community Magento Community
Magento Community
Magento Community
 
dltr_org
Sr. Member
 
Avatar
Total Posts:  167
Joined:  2008-09-12
rockdale, sydney, australia
 

i got the same issue.

i have set the category to load static block only. and everything is working. Toolbar is loading and showing sort by. But the pager inside toolbar is returning null.

any idea?

echo $this->getPagerHtml();

if i force load it through other methods i get errors like ‘getLastPageNum’ method not defined and so on…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spoker04
Member
 
Avatar
Total Posts:  33
Joined:  2009-07-09
 

Hello,

I’ve a problem with the code of the app/design.../new.php file :

$toolbar $this->getToolbarBlock();
$toolbar->setCollection($this->getLoadedProductCollection());
echo 
$toolbar->toHtml();

I have this error :

Fatal errorCall to a member function setCollection() on a non-object in C:\wamp\www\new.magento.test\app\design\frontend\template-help\theme001\template\catalog\product\new.phtml  on line 29

How resolve this issue ?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2