1

I would like to ask about product category vocabulary and different page views for parent and child terms.

This is my use case: I have to build drupal 7 commerce based site, products are organised in taxonomy called "Product categories". Lets have these terms in here:

Category1

  • Subcategory1-1
    • Product1
    • Product2
  • Subcategory1-2
    • Product3

Category2

  • Subcategory2-1
  • Subcategory2-2
    • Product4
  • Subcategory2-3

Category3

  • Subcategory3-1

I need to make this behavior: Main category list page: contains list of main categories [Category1, Category2, Category3]

Main category detail page: after clicking on main category (e.g. Category1) and page contains list of child categories [Subcategory1-1, Subcategory1-2]

Child category detail page: after clicking on sub category item (e.g. Subcategory1-1) and page contains list of products [Product1, Product2]

so in general: if you click on category without products -> display child categories list of particular parent category, and if you click on category with products assigned to them display list of products...

I was googling for a while and I don't find the right solution for my use case.

Can anybody help me with this?

3

Here's my tutorial video (I've published this step by step in many places): https://www.youtube.com/watch?v=y1jqu8JnLbU

STEP BY STEP

  1. Create a Taxonomy Vocabulary here: admin/structure/taxonomy (And create a few terms)

  2. Create a Taxonomy View here: admin/structure/views/add

You want to make sure that you choose "taxonomy" entities.

  1. Add a contextual filter for "Taxonomy term: Parent term"

Make sure to select "Default Filter" and choose "Taxonomy term from url." Also, specify a "Default validation criteria" for "Taxonomy term" and choose "Convert taxonomy name to ID" and select "Transform dashes in url..."

  1. Add a new page to this view, remove the contextual filter from this view and add a filter for "Taxonomy term: Parent term" and set it to "equal to" and leave the field there blank.

  2. Save that view and create a new view here: admin/structure/views/add

Make sure this view is a for "Content" of type "Product Display" (or whatever entity bundle has the field for your taxonomy terms).

  1. Add a contextual filter for your taxonomy term, mine was called "Product Categories"

Make sure this contextual filter displays "Contents of No Results." Also, specify a "Default validation criteria" for "Taxonomy term" and choose "Convert taxonomy name to ID" and select "Transform dashes in url..."

  1. Add a "No results" item called a "View Area" and select "Page_1" from your first view (this is the second page we created)

  2. Save this view and go back to the first view.

  3. Add a "No results" item called a "View Area" and select the second view that we just made.

  4. Make sure your title field in the Taxonomy View doesn't link to the taxonomy term, but it outputs the field as a link to "catalog/[name]" that way your catalog names are clickable :)

  5. Profit!

2

A relatively easy way to do this, would be using Views. You would need two of them:

A) First you will need to have a view display products. This view has a contextual filter (taxonomy term) that filters based on the "Products Categories" field set on the products.
B) Secondly you would set up a view that displays taxonomy terms. You need a contextual filter (taxonomy parent term). This means that you expect a term as an argument to your view, and your view will display all children of the argument.
C) You need to configure View A to display View B when there are no results. That means that if View A does not deliver any products for a given taxonomy term, View B should display all children of the given term.

I tried this locally and it works as you described in the OP.

Hope this helps

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.