8

Today I have been knocking up a layered navigation option that is not based on an attribute. It is for ratings so the front end can show nice shiny stars.

To do this I have:

  1. Created a module
  2. Rewrote catalog_block_layer_view

    <blocks>        
      <catalog>
        <rewrite>
          <layer_view>Bob_Catalog_Block_Layer_View</layer_view>
         </rewrite>                
       </catalog>
    </blocks>
    
  3. Created a class called Bob_Catalog_Block_Layer_View which extends Mage_Catalog_Block_Layer_View

  4. Created _initBlocks(), getFilters() and _prepareLayout() to override the parent classes methods. These all call the parent class at some point and then either prepend or appended extra data to what is returned.
  5. Create a filter block Bob_Catalog_Block_Layer_Filter_Rating which extends Mage_Catalog_Block_Layer_Filter_Abstract with a constructor that sets the filterModelName and calls its parent and then sets a custom template as it seems Mage_Catalog_Block_Layer_Filter_Abstract has the template name hardcoded in its constructor.
  6. Create a model Bob_Catalog_Model_Layer_Filter_Rating which extends Mage_Catalog_Model_Layer_Filter_Abstract and implements apply(), getName(), _getItemsData().

This all works fine. The nice shiny stars show in the layered nav.

I have a gut feeling that I'm missing something because when it comes to layer state template I've had to put a conditional in there to pick up the custom rating filter and alter the layout. Also some of the things I've needed to do (clone the product collection so that the load event doesn't screw up the collection counts on the pagination) make me feel like I've been swimming up hill.

So the question is have any of you got any better suggestions (or maybe a blog post wink) on how to build a custom non-attribute backed layered navigation filter?

Thanks for reading,

Si

  • I have to do it the next days and I thought to do it like this. – Fabian Blechschmidt Jan 29 '13 at 21:51
5

There's another way of doing it, you could add a normal attribute to your product which would be the rating. Then use an observer (I don't know how this rating is formed) to update the product's attribute. Or, if an observer is no option, perhaps use a cronjob which updates that attribute for every product every x minutes depending on how heavy the script is.

This way there is no Magento rewriting and altering involved, all navigation modules will still work and it's easier to maintain.

Of course the question is, is this an option for you? Where do the ratings come from?

EDIT You would have to style the stars using javascript or check on the attribute name in the template.

  • I think this covers pretty much all the options. The observer suggestion is an interesting idea. – Si Griffiths Jan 30 '13 at 16:48
  • If I recall, the standard star styling comes as just a CSS width based on average rating out of 5. 1/5=20% width, 4.3/5=86% width, etc. I doubt that needs to change... but updating the attribute value through a cron script is pretty clever. +1 – pspahn Jan 31 '13 at 9:06
2

I digged into the code and found no event. Without adding extra attributes there is nothing to make without <rewrite>

You have to add your block in \Mage_Catalog_Block_Layer_View::_prepareLayout() before the apply() (if not you could use core_block_abstract_prepare_layout_after.

And you need to add it to \Mage_Catalog_Block_Layer_View::getFilters() and there is no event too.

  • This is exactly what I found. There also appears to be no way to work around the state.phtml template. It seems a conditional branch is required to alter any of the output. – Si Griffiths Jan 31 '13 at 14:09
  • I rewrote catalog/layer_filter_category, catalog/layer_filter_attribute and catalog_resource/layer_filter_attribute to show the options all the time without using the state.phtml at the moment there is no way to remove the filters :-/ – Fabian Blechschmidt Jan 31 '13 at 14:28
  • My work had a if ($_filter->getName() === 'whatever') { } To get around showing custom state html. Its not a great solution but it is a solution none the less. Ps. Hope you are well since we last met at MDP. – Si Griffiths Jan 31 '13 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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