Page 1 of 1
index_products / nested > idea - confusion
Posted: Tue Aug 30, 2022 10:15 am
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?
Re: index_products / nested > idea - confusion
Posted: Tue Aug 30, 2022 10:56 am
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.
Re: index_products / nested > idea - confusion
Posted: Tue Aug 30, 2022 11:04 am
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...
Re: index_products / nested > idea - confusion
Posted: Tue Aug 30, 2022 11:45 am
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']));