I need assistance.
The user can type in any category names separated by commas into the admin config and then the script will split them into an array. It then compares every product in the database to see if their category name matches the category any of the category names input by the user. If so, print that product in the feed file. I have a working script already which will print the product if it's lowest level category name matches one input by the user. But, I'd like to be able to include all products in higher level categories which would only contain sub-categories.
For example, if this was a heirarchy of categories:
Computers -> Video Cards -> BFG 8800 GTX, BFG 8800 GTS, etc..
-> Sound Cards -> Creative X-FI, M-Audio Revolution, etc..
Then inputting the word computers as a selected category will add all products that are contained in sub-categories of the top-level category, computers.
Here is more code to make it easier:
$products_query = "SELECT p.products_id, p.products_model, pd.products_name, pd.products_description, p.products_image, p.products_tax_class_id, p.products_price_sorter, GREATEST(p.products_date_added, p.products_last_modified, p.products_date_available) AS base_date, m.manufacturers_name, p.products_quantity, pt.type_handler, pc.categories_id, cd.categories_name, c.parent_id
FROM " . TABLE_PRODUCTS . " p
LEFT JOIN " . TABLE_MANUFACTURERS . " m ON (p.manufacturers_id = m.manufacturers_id)
LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON (p.products_id = pd.products_id)
LEFT JOIN " . TABLE_PRODUCT_TYPES . " pt ON (p.products_type=pt.type_id)
LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " pc ON (p.products_id = pc.products_id)
LEFT JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd ON (pc.categories_id = cd.categories_id)
WHERE p.products_status = 1
AND p.product_is_call = 0
AND p.product_is_free = 0
AND pd.language_id = " . $languages->fields['languages_id'] ."
ORDER BY p.products_id ASC";
$products = $db->Execute($products_query);
This part of the code checks to see if the category name matches the one input by the user:
if(zen_google_base_categories(trim(GOOGLE_BASE_CATEGORIES), trim($products->fields['categories_name'])) == true || GOOGLE_BASE_CATEGORIES == "") { // check to see if category limits are set. If so, only process for those categories.
And this is the function above:
function zen_google_base_categories($str, $string) {
$str = strtolower($str);
$str = split(",", $str);
$string = strtolower($string);
if(in_array($string, $str)) {
return true;
} else {
return false;
}
}
I need to be able to match the product_id of the product, to the correct parent_id of the top-level category, and then finally the parent_id to the correct category_name. I do have a solution, but I am just wondering if there is a better way. Here is my solution:
function zen_froogle_category_tree($id_parent=0, $cPath='', $cName='', $cats=array()){
global $db, $languages;
$cat = $db->Execute("SELECT c.categories_id, c.parent_id, cd.categories_name
FROM " . TABLE_CATEGORIES . " c
LEFT JOIN " . TABLE_CATEGORIES_DESCRIPTION . " cd on c.categories_id = cd.categories_id
WHERE c.parent_id = '" . (int)$id_parent . "'
AND cd.language_id='" . (int)$languages->fields['languages_id'] . "'
AND c.categories_status= '1'",
'', false, 150);
while (!$cat->EOF) {
$cats[$cat->fields['categories_id']]['name'] = (zen_not_null($cName) ? $cName . ', ' : '') . zen_froogle_sanita($cat->fields['categories_name']);
$cats[$cat->fields['categories_id']]['cPath'] = (zen_not_null($cPath) ? $cPath . '_' : '') . $cat->fields['categories_id'];
if (zen_has_category_subcategories($cat->fields['categories_id'])) {
$cats = zen_froogle_category_tree($cat->fields['categories_id'], $cats[$cat->fields['categories_id']]['cPath'], $cats[$cat->fields['categories_id']]['name'], $cats);
}
$cat->MoveNext();
}
return $cats;
}
This will create an array of all the categories a product is in, which I could then use as my comparison.