Sometimes you are not able to tell which Category you are looking at as the template that you are using might have changed (or even removed!) the Categories infobox. I’ve seen a few sites which use a horizontal dropdown menu (eg Easy Menu) which looks good, but takes away the visual representation of where you are. In addition, many sites remove the “breadcrumb”…
So, just how can you find out which Category you are in?
It’s rather easy. osCommerce has a variable called “current_category_id” – it shows the ID of, guess what, the Current Category ID for the product that you are looking at. You can use this such as; <?php echo $current_category_id; ?>
In simple terms, you can use this to show the Name of the Category that you are in, or to construct a breadcrumb etc etc.
Use it to show a banner!
Recently a question was posed in the official osCommerce forum where someone wanted to show a banner on, and only on, 1 particular category. The answer given by some well meaning but ultimately completely incorrect “helper” (aka “oscommerce hack job”) was to use $cPath – well, this would work if, and only if, the category the person wanted to show the banner had did NOT have subcategories, thus;
Imagine we want to show a banner only if the Category ID is 3. With this code; <?php if ($cPath == “3”) echo “banner”; ?> the banner would show on category 3 and any if it’s subcategories. With my idea, <?php if ($current_category_id == “3”) echo “banner”; ?> the banner would show ONLY on category 3 and NONE of it’s subcategories.
Another good reason to use this might be to place a message to your buyers on some Categories – maybe you want to point them towards an upsell or a special offer?
There are lots of little pieces of code like this within osCommerce – it’s always best to try to do what you want using the best piece of code for the job – otherwise you are trying to ram a square peg into a round hole!