Teach a man to fish
Being that you haven't demonstrated any code that you have tried so far - let's show you how to figure out how to do this on your own.
This is very similar to the same process used to display "new" products on the homepage, a popular wiki article and a block that comes by default with Magento:
In the static block you will see:
{{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}
The new product block in the static block two files to be set up ahead of time:
- app/code/core/Mage/Catalog/Block/Product/New.php creates the Mage_Catalog_Block_Product_New class
- a template file in app/design/frontend/base/default/template/catalog/product/new.phtml
That's it. Nothing more than those two files are require to make it work. But, how does it work?
Simple:
- The class Mage_Catalog_Block_Product_New defines a product collection
- Before that block finishes its' render, the
beforeToHtml
method is always called, and they shoe horn in the setting of the product collection
- When the template is finally rendered, the template loops the product collection set in the block and outputs the desired products.
It does more than just that, however, and adds some complicated features like how many items to display based on the root template name.
So how do I make one of these myself?
Three steps will be required:
- Create a block in your own module which sets a product collection - this collection will presumably be filtered based on your criteria - in this case items below a certain dollar value.
- Create a template file that will make use of that product collection.
- Create a static block that you will place your block render shortcode into so that it displays wherever the static block is displayed.
A sample block class would look like:
<?php
class YourCompany_YourModule_Block_Test extends Mage_Core_Block_Abstract
{
protected function _getCollection()
{
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('price',array('lt'=>'20'));
return $collection;
}
public function beforeToHtml()
{
$this->setProductCollection($this->_getCollection());
parent::beforeToHtml();
}
}
A sample template file would look like:
<?php if($products = $this->getProductCollection()):?>
<table>
<?php foreach($products as $product):?>
<tr>
<td><?php echo $product->getName(); ?></td>
<td><?php echo $product->getDescription(); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
And then your static block declaration is very much like the one used earlier - your names of your files become quite important, obviously, so for this demonstration let's assume my module uses the block alias mycompany
:
{{block type="mycompany/test" name="products.under.20" alias="products_under_20" template="mycompany/under20.phtml"}}
Getting fancy
We could alter the above code to set the collection WHERE
criteria in the layout declaration itself from our static block (instead of having to create a new block class all the time). This is easy to do with the block declaration in your static block:
{{block pricemax="20" pricemin="0" type="mycompany/test" name="products.under.20" alias="products_under_20" template="mycompany/under20.phtml"}}
Now we can modify the collection to include the pricemax
and pricemin
vars which will now be available to us:
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('price',array('lteq'=>$this->getPricemax()));
->addAttributeToFilter('price',array('gt'=>$this->getPricemin()));