Magento Themes and Magento Extensions

Magento: Get Collection of Special Offer Products

Share

Magento: Get Collection of Special Offer Products

I recently had to create a collection of special offer products to list on the clients homepage. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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>

Comments are closed.