0

On my top level nav items instead of showing all products in that category and the ones below, i would like to show static blocks with titles of "Under £10", "Under £20" and so on.

How would i do this?

I know i have to make sure it isn't being used as an anchor and in display properties of the category to set it to use a static block instead of products.

My issue is how to actually get the products that match the titles above into a static block.

So my question. How do i pull products from the db for all the categories nested in the top level category into a static block IF they meet a certain condition e.g. price <= 10

Thanks

1 Answer 1

3

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()));
2
  • Thanks for the great explanation, ill try it out now. One question how do i define what category to get the collection from? thanks
    – Tom Burman
    Commented Feb 10, 2014 at 21:49
  • It's easy to add a category filter - see my edit for how to add the static block variables which pass through to the block -- that should give you some idea of how to specify the category id you are wanting to filter by. From there you add the category id filter to the collection! Easy!
    – philwinkle
    Commented Feb 10, 2014 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.