简体   繁体   中英

Layered navigation - filter by other categories

Using layered navigation, how can I let customers filter products based on other categories the products appear in?

I'm working on a clothing store that has products organized into categories like this:

  • Tops
    • T-Shirts
    • Dress Shirts
  • Bottoms
    • Jeans
    • Shorts
  • Swim Wear
  • Outerwear
  • Accessories
    • Belts
    • Ties

We'd like to add special collections, such as "Winter Collection" or "Interview Attire". These special categories fall under a separate root category (and have an attribute set to special value, so we can differentiate between normal categories and these special ones). All products within these will also be assigned to the main categories.

If a user is browsing the Winter Collection, they should be able to filter based on those main categories . If a jacket exists in the Winter Collection and the Outwear category, we should show Outerwear as an option to filter on. Only relevant categories should appear; for example, we won't have swim suits in the Winter Collection, so the Swim Wear category should not appear.

The idea is that we don't want to duplicate those main categories for each collection - we already know what type of clothing it is (based on the category), so our collections should be aware of that.

How can this be done?

A long-winded solution is to create a new attribute as a multi-select, and populate this with all of your categories. You would then have to go through every product and select all the 'categories' that apply.

The problem with this approach is that in the filters the categories will appear as a flat list, as opposed to a tree structure, eg:

类别过滤器示例

I think it would be worse than that. I think the filters auto-sort alphabetically?

There may be a plugin to do what you are after.

I was able to figure out a decent solution which mostly works. I'm posting it here in case someone else has a similar question.

The only drawbacks are:

  • The categories don't display in a tree structure (yet - working on this)
  • Clicking to remove the category filter backs up the category tree like usual, except it goes all the way to the root, which was unacceptable. My workaround was to make the X icon completely remove the filter instead of going to the parent category.

The Details

First step was to add a new attribute to categories called "special_type". This dropdown allows admins to select whether the category acts as a core category (per my bullet list) or a special "collection".

The next step was to override the functionality inside Mage_Catalog_Model_Layer_Filter_Category::_getItemsData() where $categories is populated. If $this->getLayer()->getCurrentCategory()->getSpecialType() == the default type, I call the original method ( return parent::_getItemsData() ). Otherwise...

I pass $this->getLayer()->getProductCollection() into a custom method which determines which standard categories this appears in. I use the following filters in my query:

  • Exclude non-standard categories
  • Exclude the currently filtered category
  • Only show the highest-level categories possible

The last two basically allow this functionality: if I'm currently filtering on Tops, only show child categories of Tops and nothing else.

The custom method returns a collection of categories matching my criteria and get assigned to $categories . The rest of the method is left intact.

Hope this helps somebody in a similar situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM