Home » » Blog / News » Web Programming » Displaying New Products in Magento with Pagination
Just recently we ran into an issue where we wanted to display all new products added to a specific category in our Magento site, with pagination. To make things more complicated, we wanted to display this by using a CMS page. The default functionality that Magento provides for new products just didn’t cut it in our case. We had to come up with something different and I am documenting it here for everyone’s benefit. This method will allow you to add new products to a Magento CMS page and pull up new products for any category you choose, or for the entire site. Whether you want to add new products to your CMS page by category or for the entire site, it’s up to you.
First, let’s set the record straight. It is possible to display a list of new products in Magento using a default block. You can do this without any coding changes or modifications to Magento itself. This code will allow you to bring up a list of new products on one of your CMS pages. It is also possible to use this to bring new products up within a template file or on your homepage, though we are not covering that here.
{{block type="catalog/product_new" column_count="6" products_count="400" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}
The above code will bring up your 400 newest products in 6 columns. You can change the column_count and products_count variable so that any number of columns or products is shown. You can display all of your newest products by changing the products_count variable to zero. OK, so this is all good, but it’s not what we want. We want to display an entire page of our new products with the pagination toolbar, and we want to display the products with a Magento CMS page. It’s quite simple to accomplish this, and here is how you can do it.
The following steps will help you to add new products to a Magento CMS page and show pagination for the products.
app/code/local/Mage/Catalog/Block/Product
Note: We do not want to modify core Magento code so we create our own path in the “local” folder
app/code/local/Mage/Catalog/Block/Product/New.php
Note: We named our file New.php which worked for our purposes.
Note: If your file is named “New.php” it will overwrite Magento’s default New.php page located in app/code/core/Mage/Catalog/Block/Product. If you are going to be using the default New.php file included with Magento in other parts of your site you may want to name your file differently.
//Code
<?php
class Mage_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_List
{
function get_prod_count()
{
//unset any saved limits
Mage::getSingleton('catalog/session')->unsLimitPage();
return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 12;
}// get_prod_count
function get_cur_page()
{
return (isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 1;
}// get_cur_page
/**
* Retrieve loaded category collection
*
* @return Mage_Eav_Model_Entity_Collection_Abstract
**/
protected function _getProductCollection()
{
$todayDate = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter('news_from_date', array('date' => true, 'to' => $todayDate))
->addAttributeToFilter('news_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayDate),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left')
->addAttributeToSort('news_from_date', 'desc')
->setPageSize($this->get_prod_count())
->setCurPage($this->get_cur_page());
$this->setProductCollection($collection);
return $collection;
}// _getProductCollection
}// Mage_Catalog_Block_Product_New
?>
Now, I’m assuming you already know how to add a CMS page to your Magento site so we will not be covering it in this post. Once you have added your CMS page, there are a couple of ways to get your new products to show up. I plan on going through both of them below.
Remember that good old block code we used to get our products to show up at first? Well we can use that again, however this time we will modify it to take advantage of the list.phtml template file. Take the following block and put it in the Content area of your CMS page.
{{block type="catalog/product_new" column_count="6" products_count="0" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/list.phtml"}}
You’ll notice that the only real difference between the code shown at the beginning of this post and the code shown above is the usage of the list.phtml template file. This file gives us access to the Magento toolbar and pager options. Some of you may have noticed a slight issue with this approach. We are displaying 6 columns of products, but the number of products per page is set to 10. Also – you may see that you actually have 15 products for sale but only one page of 12 products shows. This leads us to the second option of displaying new products with Magento.
Adding new products to a Magento page using Layout XML gives us a bit more control over what is displayed on our CMS page. In order to add Layout XML you will need to open the Design tab of your CMS page. You’ll notice a textarea entitled Layout Update XML. This box is where we can put our XML to display new products. Copy and paste the following XML into your Layout Update XML box.
<reference name="content">
<block type="catalog/product_new" name="product_new" template="catalog/product/list.phtml">
<action method="setCategoryId"><category_id>10</category_id></action>
<action method="setColumnCount"><column_count>6</column_count></action>
<action method="setProductsCount"><count>0</count></action>
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager" />
<action method="setDefaultGridPerPage"><limit>12</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>12</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>24</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>36</limit></action>
<action method="addPagerLimit"><mode>grid</mode><limit>48</limit></action>
<action method="addPagerLimit" translate="label"><mode>grid</mode><limit>all</limit><label>All</label></action>
</block>
<action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>6</count></action>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>
Let’s quickly go over what this does.
<block type="catalog/product_new" name="product_new" template="catalog/product/list.phtml">
First we set the block type and the template we are using. In our case we set the block type to “catalog/product_new” which pulls the block from the New.php file we placed in our local directory at the beginning of the post.
<action method="setCategoryId"><category_id>10</category_id></action>
setCategoryId – Next we set the Category ID. In our situation we had two Magento stores running off of two different root categories. We changed the category ID depending on which category we wanted to pull new products for. You can set this to whatever category you want, or remove the line altogether.
Note: You can put this XML on multiple pages. So you could have a page for new Children clothing and a page for new Adult clothing. You would simply need to put the same XML on each page and change the Category ID to reflect the children and adult clothing categories.
<action method="setColumnCount"><column_count>6</column_count></action>
setColumnCount – This allows us to show six products in one row on the page. We set the column count to six because this fit our design. Feel free to set it to whatever suits your purposes.
<action method="setProductsCount"><count>0</count></action>
setProductsCount – Setting the products count. Leave this set to zero in order to display all new products – change it if you only wish to limit display to a certain number.
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager" />
Adds the toolbar and pager.
<action method="setDefaultGridPerPage"><limit>12</limit></action>
setDefaultGridPerPage – Configure the toolbar to show what the number of products you wish. By default we are showing 12 products per page, which is two rows of six columns.
<action method="addPagerLimit"><mode>grid</mode><limit>12</limit></action>
addPagerLimit – These all add options to the toolbar which will allow your customers to choose how many products they want to see on one page. The XML given will allow the customer to choose to display 12, 24, 36, 48, or All products on one page.
<action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>6</count></action>
Finally, in our design, the one_column layout has to be selected in order for 6 columns to show up (otherwise there isn’t enough room).
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
Set the toolbar name.
Visit your Magento CMS page. You should see all of your new products listed with pagination.
Congratulations!
Have another way of doing this? Let us all know and post a comment! Need Magento help? Having trouble with your site? Let us help! Send us an email at support@dnawebagency.com or submit a ticket, we’d love to hear from you.
RSS feed for comments on this post. TrackBack URL


This works great!
Comment by jeremysawesome — October 28, 2010 @ 8:59 pm
It doesn’t seem to be working. I am getting an error on the page…
Fatal error: Call to a member function count() on a non-object in
/home/webname/public_html/app/design/frontend/default/webname/template/catalog/product/list.phtml on line 35
Comment by Paul — November 12, 2010 @ 2:57 pm
I got it to work but it’s positioned wrong. How do I move the new products up below my slider?
Comment by Stephanie — November 17, 2010 @ 5:30 am
Thanks a lot. Is it possible to use the filter Block on the left side to filter the displayed products.
Thanx
Comment by Kay — December 15, 2010 @ 11:42 am
working fine,
but i want to display layer navingation in left section, how it it possible in “new Products” page?
Comment by layerseen — December 16, 2010 @ 5:47 am
I’ve been trying to get this to work for 2 days… it’s returning the block (both with the content method, and the design xml), however I can’t get the category filter or the products returned to work at all.
I’m on Enterprise 1.9, is there anything different that needs to be done for EE?
Comment by Nick — December 17, 2010 @ 5:22 am
Does this work for 1.3 or 1.4? Thanks!
Comment by Larry — December 23, 2010 @ 7:13 pm
Thanks a ton! It works like a charm man.
Comment by Uday — December 29, 2010 @ 3:45 am
Hello Kay, sorry for the delay. We didn’t try positioning the filtering on the left. If we get around to it I’ll post our status. Thanks, Zach
Comment by zach — December 29, 2010 @ 8:51 am
Same as above. We haven’t tried this, if we get around to it I’ll post an update. Thanks, Zach
Comment by zach — December 29, 2010 @ 8:52 am
We’ve only tried this on Community version 1.4.1 and 1.4.0. Sorry, not sure if there’s something else going on in EE. Zach
Comment by zach — December 29, 2010 @ 8:53 am
We’ve only tested this with 1.4.1 and 1.4.0. Not sure about 1.3. Sorry.
Comment by zach — December 29, 2010 @ 8:53 am
Thanks…
It’s really help me
Comment by Razaul — January 6, 2011 @ 7:09 am
This is fantastic tutorial. Thank You
Question:
How would can we expand on this and Set Anchor to this page?
(Anchor) – Allowing to show the attribute break down of the items.
Comment by Mike — January 18, 2011 @ 7:31 pm
In the breadcrumbs, the category isn´t showed, can somebody help?
Comment by Brigitte — January 21, 2011 @ 5:10 am
Great solution
It works like a charm
Thank you !!!!!!!!!!!!!!!!
Comment by Thanos — January 28, 2011 @ 2:06 am
This solution worked great, though I would like to show layered navigation as well. I actually applied the layout update to a category and assumed it would also show layered nav.
Comment by greg — February 13, 2011 @ 11:48 am
Hi,
thanks a lot for this code!
Everything works as desired, but there is one little problem. The sorting doesn’t work. If I sort by name for example, the query string is added to the url (?dir=ascℴ=name) and it is recognized by the Toolbar.php, but the products don’t get sorted.
Thanks for your help in advance!!!
Nele
Comment by Nele — February 16, 2011 @ 4:46 am
Great, it works !
Just a little question : i have set the defaultGridPerPage to 8 and the toolbar correctly shows 8 per default, but the limit is not set in the page (i have all my news till I refresh the page with another limit…)
Comment by Oidor — February 17, 2011 @ 11:12 am
Hi again,
I would like to ask everybody (who is using this solution with pagination and “sort by”) whether the “sort by” option is working or not. I’m not sure if I made a mistake implementing it or not.
Thanks a lot for your feedback!
Nele
Comment by Nele — February 25, 2011 @ 3:54 am
this works great
Comment by manik — March 1, 2011 @ 4:15 am
New products are displaying fine. However the sort does not seem to have any effect any ideas?
Comment by Paul F — March 2, 2011 @ 8:18 am
This worked, thanks! I had to reindex my catalog to get products to show up. Before that the page was saying the category was empty.
Comment by Jonathan Mosney — March 7, 2011 @ 10:52 am
Always say there is no product matching to this query. when ever asigned some product as new product.
why this happen a can’t understand Pl help !!!!!!!!!!!
Thanks In Advanced
Comment by vipin — March 10, 2011 @ 3:09 am
its work thanks
Comment by saumil — April 7, 2011 @ 3:05 am
Not working ;(
Comment by joshw — April 12, 2011 @ 12:44 pm
Works great using New.php and
But what if I want to make multiple CMS pages like this with different product collection filters.
I tried this:
class Mage_Catalog_Block_Product_Special defined in New.php and I try to load it with:
system.log gives me this:
ERR (3): Notice: Undefined property: Mage_Core_Exception::$__toString in /home/dim/public_html/app/Mage.php on line 733
Comment by Maxime Asselin — May 5, 2011 @ 10:35 am
[...] information needed? Goto http://www.dnawebagency.com/displaying-new-products-in-magento-with-pagination [+] Share & Bookmark • Twitter • StumbleUpon • Digg • Delicious [...]
Pingback by Include Toolbar And Pagination in CMS page « Logical Infosystem — May 10, 2011 @ 12:22 am
Sorting fix for function _getProductCollection():
$order = (isset($_REQUEST['order'])) ? $_REQUEST['order'] : “name”;
$dir = (isset($_REQUEST['dir'])) ? $_REQUEST['dir'] : “asc”;
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToFilter(’news_from_date’, array(’date’ => true, ‘to’ => $todayDate))
->addAttributeToFilter(’news_to_date’, array(’or’=> array(
0 => array(’date’ => true, ‘from’ => $todayDate),
1 => array(’is’ => new Zend_Db_Expr(’null’)))
), ‘left’)
->addAttributeToSort($order, $dir)
->setPageSize($this->get_prod_count())
->setCurPage($this->get_cur_page());
Comment by ATD — May 18, 2011 @ 9:45 pm
follow you steps final i do success, and i note it in my mother language Chinaese, of couse, your copyright is in my article. thank you again
welcome to see my note: http://sjolzy.cn/Add-New-products-in-the-Magento-CMS-Page-with-paging.html
Comment by sjolzy — May 24, 2011 @ 8:54 am
muchos thanks for the sort fix sjolzy
Works with latest magento version 1.5.1.0
Comment by pob — June 3, 2011 @ 4:12 am
You’re welcome! Glad it works with 1.5.1.0
Comment by zach — June 22, 2011 @ 7:19 am
I am using this code but paging and shot not working.
when i click page number url problem add shopby like
http://abc.com/shopby/?dir=desc&order=position
correct url is:
http://abc.com/page-name/?dir=desc&order=position
why this happen a can’t understand Please help..
Comment by Suganaram — August 24, 2011 @ 6:24 am