Magento, products on sale
17 Comments 8th SEP 2009 | Posted by Tomas Novoselic in E-Commerce, Magento

Most of you probably know this, but here’s a little code snippet for every Magento beginner that needs page with products on sale listing.
So, easiest way to accomplish that is to follow these steps…
1) Make CMS page called “Products on sale”, or whatever you wanna call it…
2) Put this code:
{{block type="core/template" template="callouts/products_on_sale.phtml"}}
in CMS page you created
3) Make app/design/frontend/default/YOUR_THEME/template/callouts/products_on_sale.phtml
4) Put this code in products_on_sale.phtml
< ?php
$product = Mage::getModel('catalog/product');
$collection = $product->getCollection();
foreach ($collection as $product)
{
$result[] = $product->getId();
}
$j = 0;
$product_ = array();
foreach ($result as $_product_id)
{
$in_stock = 0;
$_product = new Mage_Catalog_Model_Product();
$_product->load($_product_id);
#var_dump(get_class_methods(get_class($_product)));exit();
$time = time();
if(strtotime($_product->getSpecialFromDate()) < time() &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; strtotime($_product->getSpecialToDate()) > time()) {
$product_[$j]['price'] = $_product->getPrice();
$product_[$j]['special_price'] = $_product->getSpecialPrice();
$product_[$j]['name'] = $_product->getName();
$product_[$j]['image'] = $_product->getSmallImageUrl();
$product_[$j]['url'] = $_product->getProductUrl();
$in_stock = $_product->isInStock();
if($in_stock) {
$in_stock = 'In Stock';
}else {
$in_stock = 'Out of Stock';
}
$product_[$j]['in_stock'] = $in_stock;
$j ++;
}
}
if(empty($product_)) {
echo $this->__('There is no product on sale');
}
foreach($product_ as $item)
{
?>
<div class="Item">
<a href="<?php echo $item['url'];?>"><img src="<?php echo $item['image'];?/>" alt="< ?php echo $item['name'];?>" /></a><br />
<a href="<?php echo $item['url'];?>">< ?php echo $item['name'];?></a><br />
<span class="RegularPrice">< ?php echo $item['price'];?></span>
<span class="SpecialPrice">< ?php echo $item['special_price'];?></span>
<div class="Stock">< ?php echo $item['in_stock'];?></div>
</div>
< ?php
}
?>
Style it at will ![]()
Enjoy!
P.S. Of course you need to add some products on sale ![]()
itemsonsale
To post code in comments, place your code inside [code] and [/code] tags.


















September 10th, 2009 at 2:17
I love how your var_dump is still there and commented out! Awesome.
September 10th, 2009 at 7:33
Lol, I totally forgot it btw. there is 1 so called error…
This line:
if(strtotime($_product->getSpecialFromDate()) < time() && strtotime($_product->getSpecialToDate()) > time()) {
Should be $time instead time() …
There is some formating issues too
September 11th, 2009 at 11:47
Love the work and information you guys provide. Thanks a lot.
September 11th, 2009 at 22:16
first i got this error: Parse error: syntax error, unexpected ‘?’ in on line 50 and i deleted the ‘ / ‘ after that i get this error:
Notice: Undefined variable: item
And with this error i could use some help
thanx!
September 17th, 2009 at 0:12
Would it be possible to reference the block from the XML that updates a page layout? I’m trying to call it inside my home page but nothing shows…
Thanks for you simple samples!
September 21st, 2009 at 10:16
Fine trick, thank you.
There are some commercial extension help to indicate your “on sale” products with attractive labels (for example http://ecommerce.aheadworks.com/extensions/on-sale.html )
September 23rd, 2009 at 23:47
This looks pretty cool, but I have the same error as Richard.
September 24th, 2009 at 7:29
Ok guys I uploaded working sample… this wp code formater just breaks code.
September 28th, 2009 at 17:56
Awesome! Thanks.
October 6th, 2009 at 15:35
May this code be a performance killer when having thousands of products? I mean, you’re looping over each of them!
Sorry but this code is not for productive purposes… It would be better to filter them directly by an sql query. Have a look at Mage_Catalog_Block_Product_New and adapt this kind of getting products on sale.
October 9th, 2009 at 13:22
This is the best example of how to not write Magento code.
November 6th, 2009 at 11:04
great! but how do you create an addto cart link? like if $this->getAddToCartUrl($product_[0]) doesn’t seem to work??
February 23rd, 2010 at 11:09
Thanks. You are my hero!
February 23rd, 2010 at 15:31
Yeah also having problems with the getAddToCartUrl
March 3rd, 2010 at 14:21
I have only a problem: if I have a Configurable product with, for example, 3 variant simple product, it show all 4…….I want to see only the configurable……I’m doing something wrong?
May 31st, 2010 at 5:22
the code works great and all but I have one minor problem which is related to the 4 decimals behind the price… the price shows like this for example a products that costs 200$ shows 200.0000 using your code.
What should I change/add in the code to remove the extra decimals?
thanks in advance!
July 29th, 2010 at 9:38
Only thing left is to apply layer to get pagination