creative-mountains

Magento - Show Only Special Priced Products In A Category

So your tired of always having to go through your catalog to add and remove products that have a special price to a certain category called On Sale or something similar. A solution is on hand now that will allow you to filter out those Special Priced products that have a special from and special to date.

First of all this is the code were that we're going to be using to do this.

<?php
Mage
::getSingleton('core/session', array('name' => 'frontend'));
$_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
->
addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->
addMinimalPrice()
->
addStoreFilter();

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($_productCollection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($_productCollection);

$todayDate = date('m/d/y');
$tomorrow = mktime(0, 0, 0, date('m'), date('d'), date('y'));
$tomorrowDate = date('m/d/y', $tomorrow);

$_productCollection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
->
addAttributeToFilter('special_to_date', array('or'=> array(
0 => array('date' => true, 'from' => $tomorrowDate),
1 => array('is' => new Zend_Db_Expr('null')))
),
'left');
?>
<div class="listing-type-grid catalog-listing">
<?php $_collectionSize = $_productCollection->count() ?>
<?php $i
=0; foreach($_productCollection as $_product):?>
<?php
if($i++%3==0): ?>
<ol class="grid-row">
<?php endif; ?>
<li class="item">
<
p class="product-image">
<
a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
<
img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>" width="135" height="135" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
</
a>
</
p>
<
h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h5>
<?php if($_product->getRatingSummary()): ?>
<?php
echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php
endif; ?>
<?php
echo $this->getPriceHtml($_product, true) ?>
<?php
if($_product->isSaleable()): ?>
<button type="button" class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>
<?php else: ?>
<div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
<?php endif; ?>
</li>

<?php if($i%3==0 || $i==$_collectionSize): ?>
</ol>
<?php endif; ?>
<?php
endforeach ?>
</div>


You can then paste this into a file and name it specials.phtml and save it into your frontend template folder catalog/product.

Once you've done this you can then go into your admin and go to Manage Categories.

Create a category called On Sale Items or something else that you find fitting for your website. On the tabs click on Custom Design and in the Custom Layout box you can place the following.

<reference name="content">
<remove name="product_list"/>
<remove name="category.products"/>
<block before="-" type="catalog/product" name="home.new" alias="product" template="catalog/product/specials.phtml">
<action method="setProductsCount"><count>9</count></action>
</block>
</reference>

This will remove the current content block and replace it with your specials.phtml content. Press save category and thats it. You will now have a category that will only have your current special priced products.
creative-trees