index_products / nested > idea - confusion

Open to all! Ask other shopowners for help.
Post Reply
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

index_products / nested > idea - confusion

Post by loop »

Hi All

in category_tree.php is this $category_depth created:

Code: Select all

$GLOBALS['category_depth']
          = (count($GLOBALS['category_tree']->get_children($GLOBALS['current_category_id'])) > 0)
          ? 'nested'
          : 'products';
My Problem or Idea is, whenn a category have subcategories BUT also have products, it's index_nested (that's not wrong, but it's also has products)

and i need in different places to decide we are on a category with products or not to know if we should display filters / banners or whatever.

would it be a problem to make the category_depth the way: look if the category has products, whenn yes its index_products, if no look if it has nested categories, if yes = index_nested and if it's a empty category with no products it can also be "index_products but with 0 entries)

what you think?


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: index_products / nested > idea - confusion

Post by ecartz »

If you have a category that has both categories and products, it is going to be confusing.

It used to be the way that you describe. The problem then is that it is a pain to tell if there are subcategories if there are products. And it's almost impossible to fix, because the categories model is then fundamentally wrong. Fixing the problem of showing products in index_nested is much easier than fixing the problem of showing categories in index_products. Or simply don't put products in categories with subcategories.

This way is simpler. There's top, where you have no parent categories. There's nested, where you have subcategories. There's products, where you have no subcategories. None of those categories tell you anything about whether or not there are products in the category (products might seem to do so but doesn't).

If you want to display products in a nested category, go ahead. Copy cm_ip_product_listing.php to cm_in_product_listing.php with all the associated files and the constants changed. Or reuse the IP constants but remove them from get_parameters and change the CONFIG_KEY_BASE.

Similarly, if you want to know whether or not there are products in a category, go ahead and do that. But we're not going to redefine the category_depth to cover that. Make your own variable, e.g. $has_products for that.
loop
Contributor
Posts: 253
Joined: Thu Mar 25, 2021 12:26 pm
Phoenix Version:
Has thanked: 7 times
Been thanked: 3 times

Re: index_products / nested > idea - confusion

Post by loop »

thank you ecartz

what would be the simplest (cheapest load / cpu) way to detect if a category has products and thenn make my own variable? make a DB query with numrow on products_to_categories?

and would you place the lines also in the "set_global_depth" function?

Code: Select all

public static function set_global_depth() {
      // the following cPath references come from application_top.php
      if (isset($GLOBALS['cPath']) && !Text::is_empty($GLOBALS['cPath'])) {
        $GLOBALS['category_depth']
          = (count($GLOBALS['category_tree']->get_children($GLOBALS['current_category_id'])) > 0)
          ? 'nested'
          : 'products';
      } else {
        $GLOBALS['category_depth'] = 'top';
      }
    }
because i'm not sure reayly where this set_global_depth is called and how often...
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: index_products / nested > idea - confusion

Post by ecartz »

Put it wherever you first need it. Or you could add a hook to do it in parallel with set_category_depth. That's one of the system hooks in the database. https://github.com/CE-PhoenixCart/Phoen ... .sql#L1141

Code: Select all

$GLOBALS['has_products'] = empty($GLOBALS['current_category_id']) 
                         ? false
                         : (bool) mysqli_num_rows($GLOBALS['db']->query(sprintf('SELECT 1 FROM products_to_categories WHERE categories_id = %d LIMIT 1', $GLOBALS['current_category_id']));


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply