I need the parent category ID to which a product belongs, to use in a plugin (Product Attributes) that apparently gets called so early it does not have access to any of this data yet, except for the product id.
So what functions do I make \ call to get the parent category info from the product ID number?
I've tried just about everything I can think of, but I can't make it work.
Can anyone here give me a hand?
» Quick.Cart v1.x
-DmD-
merci
2007-10-15 09:37
Proxivire - I'm not sure where exactly you need this parent category ID, but probably it should works - the id of parent's product is store in array, the problem is that it can have more then 1 parent category, so if it has one parent it is: $aData['aCategories'][0] = 'category id'; if it has two prent category: $aData['aCategories'][1] = 'second category id'; and so on...
Proxivire
2007-10-15 14:43
merci,
Thanks for the response, sadly, this doesn't work, those variables (and all the others that are normally associated with reading out a product's info) aren't set at that point in the script yet, it seems. Only the product ID ($iProduct) is set.
So what I actually want to do is to read the parent category (so not the category that the product is in, but the parent category of that one) out of the database files and set it in a variable. I just don't know how, can you help me with that?
-DmD-
merci
2007-10-16 08:32
Proxivire - you can try this: as you said, you have id of the product, so you can use function throwProductCategories( 'products_id' ); - it returns array of this products categories. Don't use this function too often, as it isn't too efficient. By the way in documentation is short description of all functions.
Proxivire
2007-10-18 12:53
merci,
Thanks for the tip, I've tried that before, but it didn't work then, after messing around with it yesterday I found out that I had to use this to make it work: require_once DIR_CORE.'products-ff.php'; require_once DIR_CORE.'products.php'; $categoryidarray = throwProductCategories($iProduct);
After I included that in the code, the function did work, but that only gave me the category the product was in, instead of the parent category. Then, after alot of tinkering I managed to workout a way to get what I wanted, (what I want is to add attributes to all products in a given (parent)category), by using this code:
// this... require_once DIR_CORE.'products-ff.php'; require_once DIR_CORE.'products.php'; // ...is to enable the use of the throwProductCategories function
// this... $categoryidarray = throwProductCategories($iProduct); $categoryid = $categoryidarray['0']; // ...is to get the category id into an array, and from that array into a variable
// this... if (isset($categoryidarray['1']) !== false) { $categoryid = $categoryidarray['1']; } if (isset($categoryidarray['2']) !== false) { $categoryid = $categoryidarray['2']; } // ...is to always use the last set category id, since that's the one I'll use (I never have a product in more than 3 categories).
// this... $categoryinfo = get_object_vars($GLOBALS['oCategory']); // ...pulls the "Categories" object out of the $GLOBALS variable, into an array
// this... $parentid = $categoryinfo['aData'][$categoryid]['iParent']; $parentposition = $categoryinfo['aData'][$parentid]['iPosition']; $parentname = $categoryinfo['aData'][$categoryid]['sName']; // ...pulls relevant data out of the above array and puts them in seperate variables
After doing the above, I have all the data I need to check and manipulate attributes for any category I want, now I just need to know if what I did above is proper programming, using the $GLOBALS array and all, and maybe other problems I haven't thought of, like the "require_once" parts.
It works well, I'd just like to know if there are any problems with the code, can you tell me if there are or aren't, or other suggestions?
-DmD-
Proxivire
2007-10-22 14:48
merci,
Not to bother you too much, but do you have an answer for me? If there's no problem with the code in this way, I'd like to get it worked into our site :)
-DmD-
merci
2007-10-23 08:46
Proxivire - yes, your code is ok, there shouldn't be problem with using $GLOBALS array.
Proxivire
2007-10-23 09:30
Thanks a bunch,
Last questions, if you have the time: I take it that also means there aren't any bottlenecks in the require_once usages and such? ( I thought it might be overkill to load two full php files like that)
Before you said that throwProductCategories() is inefficient, just out of curiousity; howcome, and is there anything that can be done for it, since with this code it'll be used again for each product viewed?
And do you have any input on this question of mine? http://opensolution.org/Quick.Cart/forum/?p=readTopic&nr=4530
-DmD-
merci
2007-10-24 08:55
Proxivire - if about require_once - it is loaded only once, we also use this method. Secondly I have said that if you will use too often throwProductCategories(), it can make your application less efficient, but if you use it once or twice it's ok. I can't tell you if this code is efficient or not. To check it you should make some tests and it depends on how many catgories and products do you have, how fast is your server and how many space do you have on your server...to many questions to said if it is efficient. In my opinion if you have not too many products, you don't need to bother.
Proxivire
2007-10-27 11:33
merci,
Sorry for my late response. We have a bit over 2000 products, but everything seems to run well, even at peak hours.
You've contributed to making our site more usable yet again. Thanks again :)