In UC6, if a catalog category existed but did not contain any products it would not be displayed. Is this possible un UC7? I am very familiar with Views but I can't see a way to set it to not display a category term if not products exist within it's hierarchy.

To complicate matters, our categories are hierarchical and may be several levels deep. So we would need a way to check through each level to see if there are any products in it and, if not, not display the top highest level category which does not have products in its children.

Possible?

Comments

end user’s picture

3CWebDev’s picture

Thank you for the information. I have tried these solutions but they only work for the lowest level terms in a tree. It does not function correctly if there is a hierarchy tree structure. In my case, enabling the "content with term" relationship hides all of the catalog terms because there are no products in 1st nest terms.

Example:

Catalog->Top Level Term->Second Level Term->Products

Because the products are several terms deep it will hide all terms.

This issue is compounded when using the catalog block because the block automatically filters out empty terms but the pages do not. Both catalog types should match (like UC2)

I have spent more than several hours working on this but haven't had any luck. Any ideas?

longwave’s picture

You may be better off moving or repeating this question in the Views issue queue, as this is not really specific to Ubercart - any solution would also apply to any taxonomy term listing in Views.

TR’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Jeremy Byrne’s picture

Issue summary: View changes

This issue is not Fixed. Comment #3 merely suggests seeking a solution elsewhere, even though this is in no way an Issue for the Views module.

After extensive research, and once it had become apparent that there was no simpler way to do it (eg. one which involved no additional Contributed Modules), I created my own solution based on discussion on this thread linked from Comment #1:

  • Install and enable Views PHP.
  • Edit the uc_catalog_terms view.
  • Add a standard Filter (ie. under Filter criteria) of type Global: PHP, setting the Filter code to:
    $count = db_query("SELECT COUNT(*) FROM {taxonomy_term_hierarchy} WHERE parent = $row->tid")->fetchField();
    if ($count == 0){
      $count_2 = db_query("SELECT COUNT(*) FROM {taxonomy_index} AS ti INNER JOIN {node} AS n USING (nid) WHERE n.status = 1 AND ti.tid = $row->tid")->fetchField();
      if ($count_2 == 0){
        return TRUE;
      }
    }
    

This filter will remove all terms which both have no subterms and are associated with no publishedproducts (ie. which are truly "empty categories"). If terms associated with unpublished products are to be included, the INNER JOIN can be removed (ie. all the text from "AS ti" to "AND ti." inclusive, except the WHERE keyword).

TezMan’s picture

Issue summary: View changes
TezMan’s picture

Issue summary: View changes
TezMan’s picture

TR’s picture

Please don't delete the issue summary. If you want to comment on the issue, enter your comments in the "Comment" section.

TR’s picture

Issue summary: View changes

Restored issue summary.

Jeremy Byrne’s picture

TezMan: Thanks for responding to my suggested fix (even though you inadvertently overwrote the issue). You've indicated an important problem with my suggested filter code (when you say "this filter doesn't remove categories under which subcategories exist under which no (active) nodes exist"), but your suggested change doesn't actually fix that problem, which can really only be addressed using recursive code (because we can't know how far down a hierarchy of productless categories we might have to search before we find a category with a product in it).

Here is my amended solution:

  • Install and enable Views PHP.
  • Edit the uc_catalog_terms view.
  • Add a standard Filter (ie. under Filter criteria) of type Global: PHP, setting the Setup code to:
    function should_we_filter_this_term($term_id = 0) {
    
      // Are any published nodes tagged with this term?
      $count = db_query("SELECT COUNT(*) FROM {taxonomy_index} WHERE tid = $term_id")->fetchField();
    
      // If so, don't filter the term.
      if ($count > 0) {
    
        return FALSE;
      }
    
      // If not, does this term have child terms?
      else {
    
        // Collect the terms which have this term as their parent.
        $result = db_query("SELECT tid FROM {taxonomy_term_hierarchy} WHERE parent = $term_id");
    
        // If there aren't any, filter the term.
        if ($result->rowCount() == 0) {
    
          return TRUE;
        }
    
        // If there are, check each child term using the same critria.
        else {
    
          foreach($result AS $record) {
    
            // If a child term comes back not filterable, then don't filter the parent.
            if(!should_we_filter_this_term($record->tid)) {
    
              return FALSE;
            }
          }
    
          // If we've gone through the child terms without getting a "not filterable", filter the parent.
          return TRUE;
        }
      }
    }
    

    and setting the Filter code to:

    return should_we_filter_this_term($row->tid);
    

Also, after further research, it turns out that the {taxonomy_index} table does not include unpublished nodes, and the Drupal 7 catalog doesn't display unpublished nodes at all. As such, my comment above about adjusting the code to display unpublished nodes should be ignored.

liezie_D’s picture

#12 worked perfectly! many many thanks!!

IanBezanson’s picture

If you want to adjust this to account for categories with published items, change the setup code to:

function should_we_filter_this_term($term_id = 0) {

  // Are any published nodes tagged with this term?
  $count = db_query("SELECT COUNT(*) FROM {taxonomy_index} ti JOIN {node} n ON ti.nid = n.nid WHERE ti.tid = $term_id AND n.status")->fetchField();

  // If so, don't filter the term.
  if ($count > 0) {

    return FALSE;
  }

  // If not, does this term have child terms?
  else {

    // Collect the terms which have this term as their parent.
    $result = db_query("SELECT tid FROM {taxonomy_term_hierarchy} WHERE parent = $term_id");

    // If there aren't any, filter the term.
    if ($result->rowCount() == 0) {

      return TRUE;
    }

    // If there are, check each child term using the same critria.
    else {

      foreach($result AS $record) {

        // If a child term comes back not filterable, then don't filter the parent.
        if(!should_we_filter_this_term($record->tid)) {

          return FALSE;
        }
      }

      // If we've gone through the child terms without getting a "not filterable", filter the parent.
      return TRUE;
    }
  }
}
Sigor07’s picture

Hi, IanBezanson.

I am posting to see if you can help me out.

I have a taxonomy called "Folder" and have nested terms inside and also an Entity Reference field "Assigned to" that takes care of what users have been assigned to a particular node.

I created a taxonomy View using the Views Tree module and also displaying this View in a separate tab belonging to the user profile along with a Views Field View field that shows the content inside the folders (terms).

The code provided previously has worked with showing terms that actually have something in them but now I am trying to show only folders (terms) that both have something AND have been assigned to the current user.

So, if I have "Folder 3" inside of "Folder 1" and "Folder 2", do not show them unless they have content assigned to the current user.

Do you have an idea of what code I could use to achieve this?