Display special priced products on your Magento homepage
In my previous blogpost, I covered how you can create a €1 Bargain page in Magento. To do so, we learned how to create a custom block on wich we applied price as a filter on the product collection and how to create a XML Layout update for a random CMS page. The main advantage of this method for creating custom catalog pages in Magento is that the toolbar, pagination, navigation layered, caches & indexes all continue to function correctly. In this post, I want to show you how you can use this same method to display all products with a special price on the frontpage of your Magento webshop
Open your homepage CMS page and click on the design tab. Paste the following:
Because my frontpage has 3 columns, you can see I have set the amount of columns in the grid view to 3 using the action method "addColumnCountLayoutDepend". I have set 2 as the categoryId, because this the id of my root category. Be sure that you use the correct id of the category you want to display and be sure that it's set to be an Anchor (or it won't display any products in the first place).
In the XML update above, you can see we call catalog/product_specials as the block type. What we are going to do now, is creating a custom block where we actually apply our custom pricefilters. To do so, make a copy of /app/code/core/Mage/Catalog/Block/Product/List.php, create the directory /app/code/local/Mage/Catalog/Block/Product and place it there. Rename this file to Specials.php
Edit Specials.php and change the class definition (the first line of actual code, line number 35):
Scroll to around line 89, and you'll find the following definition
After this definition, we can do some magic. Place the following code just below it:
As you can see, we just define today's date and tomorrow's date and then apply the special_from_date and special_to_date as attribute's to filter. Because we can imagine you will not always set a 'to' date when giving your products a special price, we use Zend_DB_Expr to say it should also add products to the product collection that don't have a 'to' date configured. If you do wan't to display only the products that have a value for both the from and to date, use this code instead:
How to limit the amount of products to display
Imagine you want to display not ALL products in a certain category, but want to limit the amount to let's say 5 (randoms if you want). For example: you want to create a featured products block in Magento. To do so, you can use this exact same method (copy the List.php, name it Featured.php' etc. etc) and you will only have to make some small changes to your XML:
Now, pay attention to what I'm doing here, because this example won't just work when you copy paste it. First, you'll see that I indeed call my custom block catalog/product_featured and use a custom template to display the results: catalog/product/featured.phtml. You can use list.phtml here if you want to do a quick test. Now on line 4, you'll see that I give the toolbar a custom name: product_featured_toolbar and that I use an action method on the next line to set the addPageLimit for listview to limit the results to 5 products. If you want to use a custom gridview, you can change the mode to grid. Last but not least, on line 7 I use the action method setToolbarBlockName to identify the custom toolbar so that my addPagerLimit will only affect the toolbar from this specific block. If you don't use a custom name for your toolbar, you'll see that all other listviews on the page will be affected and in most cases you probably do not want that.