Zen Cart Logo
Forums / Zen Cart Code Suggestions / [Done 1.4.0] Correct category determination

[Done 1.4.0] Correct category determination

Locked

Views: 1,812

Results 1 to 2 of 2
This thread is locked. New replies are disabled.
10 Mar 2008, 03:27
#1
capnhairdo avatar

capnhairdo

New Zenner

Join Date:
Jan 2008
Posts:
38
Plugin Contributions:
1

[Done 1.4.0] Correct category determination

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):

  // 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";
03 May 2008, 06:34
#2
focus360 avatar

focus360

New Zenner

Join Date:
May 2006
Posts:
76
Plugin Contributions:
0

Re: [Done 1.4.0] Correct category determination

Thanks a lot for posting this solution. It has helped me a lot. :clap: