문제

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

도움이 되었습니까?

해결책

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 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top