Magento, products on sale

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

Magento, products on sale

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

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 17 comments (Add Yours +)

  • I love how your var_dump is still there and commented out! Awesome.

  • 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 :)

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

  • richard van Zanten Says

    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!

  • 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!

  • 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 )

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

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

  • Awesome! Thanks.

  • 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.

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

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

  • Thanks. You are my hero!

  • Yeah also having problems with the getAddToCartUrl

  • 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?

  • Cristian Says

    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!

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

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009