Magento, products on sale

Featured Image

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;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 :D
itemsonsale

23
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Tomas Novoselic

Team Leader / Senior Developer

At Inchoo, Tomas is a Team leader and Certified Magento Developer. He handles Magento modifications at any level. He also works closely with clients on Magento projects of any size and difficulty.

Other posts from this author

Discussion 23 Comments

Add Comment
  1. I love how your var_dump is still there and commented out! Awesome.

  2. 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 :)

  3. Love the work and information you guys provide. Thanks a lot.

  4. richard van Zanten

    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!

  5. Agus

    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!

  6. Cyril

    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 )

  7. This looks pretty cool, but I have the same error as Richard.

  8. Ok guys I uploaded working sample… this wp code formater just breaks code.

  9. Awesome! Thanks.

  10. Robert

    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.

  11. W

    This is the best example of how to not write Magento code.

  12. juan

    great! but how do you create an addto cart link? like if $this->getAddToCartUrl($product_[0]) doesn’t seem to work??

  13. Thanks. You are my hero!

  14. Yeah also having problems with the getAddToCartUrl

  15. 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?

  16. Cristian

    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!

  17. Srdjan

    Only thing left is to apply layer to get pagination :)

  18. Very useful snippet of code i shall use this on my magento website many thanks .

  19. chintan

    I think its not working with 1.5.. any idea how to implement it..?

  20. Jim

    Great tutorial, thanks!

    Sorry if this is a stupid question, but is there any way to develop this code further so that you can list sale items from just one category?

  21. jamuna

    Really its work nice buddy thank u so much for ur great help!

  22. jamuna

    Dude its only show the product one by one. I want to show 3 or 4 products in each row

  23. Janis

    Yes nice tutorial. Keep up the good work. ;)

    P.S. Highlighter is awful.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top