Home Magento Guide

Magento, products on sale

16 October 2009 No Comment

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>
<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>< ?php echo $item['price'];?></span>
<span>< ?php echo $item['special_price'];?></span>
<div>< ?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

By Tomas Novoselic from Inchoo.net


Related posts:

  1. More Flexible Approach for Listing Sale Products in Magento
  2. Adding simple Tweet this for Magento products
  3. How to fix the Magento Out of stock bug, version 1.1.x
  4. Display Promotion & Random Products in Magento
  5. Some custom Blocks to help you show products

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree

Security Code: