Thursday 19 January 2012

How to get list of all special offer products in Magento

Here is the complete details to get display the list of all specila offer products in Magento

There’s a few different attributes that we need to filter to get the proper results.

1. The products visibility must NOT be set to 1. This means that the product is going to be visible individually. If we tried to link to a product that was not visible individually we might get a 404 or even worse, the mage error screen! See this post for a list of visibility options ->addAttributeToFilter(‘visibility’, array(‘neq’=>1))
2. In my case I don’t want to show products that have an empty special price field. ->addAttributeToFilter(‘special_price’, array(‘neq’=>”))
4. I want to set the number of products returned to 8. ->setPageSize(8)
5. Finally I set up my date filters.



<?php
$todayDate  = Mage::app()->getLocale()->date()->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$special= Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('visibility', array('neq'=>1))
    ->addAttributeToFilter('special_price', array('neq'=>''))
    ->setPageSize(8) // Only return 4 products
    ->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
    ->addAttributeToFilter('special_to_date', array('or'=> array(
           0 => array('date' => true, 'from' => $todayDate),
           1 => array('is' => new Zend_Db_Expr('null')))
           ), 'left')
    ->addAttributeToSort('special_from_date', 'desc');
$special->load();
?>
  
<ul class="list clearfix clear" id="special">
    <?php $x = 1; ?>
    <?php foreach ($special as $product): ?>
        <li class="span-1 left a-center <?php if ($x ==8) : echo 'last'; endif; ?>">
            <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $product->getName() ?>"><img class="a-center" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(120); ?>"/></a>
            <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $product->getName() ?>"><h2><?php echo $product->getName(); ?></h2></a>
            <strong><?php echo $this->getPriceHtml($product, true); ?></strong>
        </li>
<?php $x++;
    endforeach; ?>
</ul>
 
 
 
 
 Do this....
 

No comments:

Post a Comment