Magento, products on sale

15 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
  • FriendFeed
  • NewsVine
  • Sphinn
  • Tumblr

There are 15 comments (Add Yours +)

Leave a Comment

Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009