February 27, 2020

cm-mini

Thinking Magento

Magento - Show Only Special Priced Products In A Category

THIS TECHNIQUE HAS BEEN UPDATED - CLICK HERE FOR A BETTER WAY OF MAKING A SALE 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'); 
echo '<div class="listing-type-grid catalog-listing">';
$_collectionSize = $_productCollection->count();
$i=0; foreach($_productCollection as $_product):
if($i++%3==0) 
echo '<ol class="grid-row">';
echo '<li class="item">';
echo '<p class="product-image">';
echo '<a href="'.$_product->getProductUrl().'" title="'.$this->htmlEscape($this->getImageLabel($_product, 'small_image')).'">';
echo '<img src="'.$this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135);.'" width="135" height="135" alt=".$this->htmlEscape($this->getImageLabel($_product, 'small_image')).'" title="'.$this->htmlEscape($this->getImageLabel($_product, 'small_image')).'" />
</a>
</p>';
echo '<h5><a href="'.$this->getProductUrl().'" title="'.$this->htmlEscape($_product->getName()).'">'.$this->htmlEscape($_product->getName()).'</h5>';
echo $this->getPriceHtml($_product, true) ?>
echo '</li>';
echo '</ol>';
echo '</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.