Apologies for cross-posting this excerpt from
another, longer post, but it seemed to merit being posted here:
When ZC doesn't know the category you want—because no category is defined in the URL (cPath)—then init_category_path.php runs zen_get_product_path() to determine and set it.
zen_get_product_path() (in functions_categories.php) does a general query of the products_to_categories table to find all product/category associations. However, this query has a limit=1, so it only gets the first record it finds there. For products in only one category, this is fine; but if a product is in multiple categories, then the category it returns is essentially random. Which defeats the purpose of a "master" category. If a category is going to be determined automatically by ZenCart, it should be the master category for the product, not a random one.
This change to functions_categories.php fixes this (additions to original code in red):
Code:
// first, try to get master category id
$category_query = "select p2c.categories_id
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
where p.products_id = '" . (int)$products_id . "'
and p.products_status = '1'
and p.products_id = p2c.products_id and p.master_categories_id = p2c.categories_id";
// in case the master category is invalid, fall back on getting first product/category association instead
$category_query .= " union distinct select p2c.categories_id
from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
where p.products_id = '" . (int)$products_id . "'
and p.products_status = '1'
and p.products_id = p2c.products_id limit 1";
Bookmarks