Support: 071 744 0084
Contact
071 744 0084
info@h-o.nl
Jos Verhaar - Magento support
Erwin Otten - New business
Paul Hachmang - Techniek

Display special priced products on your Magento homepage

By Erwin Otten 2 augustus 2010 Laat een reactie achter Ga naar reacties

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

Step one
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).

Step two
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

Step three
Edit Specials.php and change the class definition (the first line of actual code, line number 35):

to

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.

Derek Johnson

Works well, thank you. I needed to correct a typo in the XML. It should say type="catalog/product_specials" not type="catalog/product_bargains"

Admin

Thanks, fixed it!

Kay Beissert

Thank you verry much. This works perfect. Is possible to disply the filter-block on the left side to filter the displayed products?

Thanks

ktsixit

I did exactly what you suggested but no products appear.
I did't add the last part ("How to limit the amount of products to display") but I guess that it would be enough.

Conor

I have the same problem and I can not get them to show on the home page at all, please help. I am running 1.4.2.0

Erwin Otten

That is correct, you don't have to add the addPagerLimit in to see products showing up. Have you used the correct categoryId of your parent category? Have you actually gave some products a special date?

Katapofatico

Thaks for your contribution, Erwin. And... what about catalog rules special priced products? Have you any idea on how to list them, or even include them in this module?
Thanks again!

izdelava spletnih strani

TNX for this great article!

Tjerco

Hi Guys,

This working fine for me (release 1.5). I do have a small question? In which statement of the new specials.php is the number of products given back? I have a dubble statement on the frontend now.

Thanks,
Tjerco

Paul Hachmang

I received the following screenshot: http://cl.ly/8wpL

You could try and remove from the XML
<code>
< 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" >
</ block >
</code>

If that doesn't work, look in your list.phtml template.

Paul Hachmang

Could you please provide a screenshot?

Francesco

Hello,
this is a great tutorial, thanks! I have used it to create a static "Promotions" page.
For the home page, I would rather like to diplay the two last products with a special price using a custom block.
But it is not clear to me where the XML code must be inserted...can you please explain me the last part of your tutorial?

Thanks a lot in advance,
Francesco

amadex

Very, very good tutorial!!!! One doubt still: layered navigation keeps "considering" all the products of the category and not only the "bargain" ones. Any help?

Alberto

Amadex, then you could do also filering by category, byt this code:


->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id))

Marko

Thanks. This code is working perfect!

Alberto

Thank you very much! you helped me to implement some custom product listing ;)

Phil

Thanks, the code works well, great tutorial!

David

Hey, this worked great. Thanks for that. The code shows 2 products with special prices when I got it all done. However, I have 10 products that I've marked with special prices, all with the same date range. Any idea how I can debug to figure out why only 2 of the 10 are showing up?

brijin

to set limit add ,
<action method="addPagerLimit"><mode>list</mode><limit>5</limit></action> to CMS/design page guyz.....

Filip

Worked great! Thank you very much!

(probably common) mistake that bugged me:
- enter the default category id in the xml update code (it's in Manage Categories -> you have to click on the wanted category and the id's by the category name)

Christian Ellsworth

This solution works but the products always drop a day early. For example with this solution if I scheduled a product to end the special price today it will not show but will still be on sale until the end of the day.

Andrew

Hi, I'm pretty new with Magento. I'm trying to figure out this first instruction: "Open your homepage CMS page and click on the design tab". Couldn't find it anywhere. I tried going to CMS > Pages but couldn't see a homepage page. Should I create a new page then? Thanks!

Alberto

Excellent solution, works great. How do I include in the filter discounted products with the rules of the catalog?

Hobbyjunglen

Fantastic! thanks for an absolutely perfect tutorial! :)
One question: How do we do the exact same, only with NEW products (latest products from a given from/to date)? :-)

Comcol.nl

This works nicely, thanks for the information.

However, we have a situation where sometimes the special_price is erased (nullified) but the special_from date is kept. In such a situation, the product will show up on the specials page, although it is not actually a special price ;)

To fix this, add
$this->_productCollection->addFinalPrice()->getSelect()->where('price_index.final_price < price_index.price');

to the code in Special.php (after the code from Erwin) and only 'real' special offers will be found.
(credits go to inchoo.net where this code was found buried in some samples. I didn't find it elsewhere)

Regards,
Comcol.nl webmaster

Laat een reactie achter