What Category are you looking at?

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!

5 Replies to “What Category are you looking at?”

  1. if ($current_category_id == “3”) echo “banner”;

    You need to get the number of the category, eg: 2_3_15 or wahtever.

  2. Your code wouldn’t work without actually putting in the subcategory as well. I made an if statement to change the banner on one paticular ctegory and sub-categores and I could only get it to work with this category and it’s first category….I could add more sub-categories, but then I would have to add more OR statements….

    here is my example…

    if ($cPath == “1” OR $cPath == “1_19”)

  3. This is because the code is only meant for use in one category. For more categories, use a switch – also covered elsewhere in this blog…

    switch ($current_category_id) {
    case 1:
    case 19:
    echo “banner”;
    break;
    default:
    echo “whatever”;
    break;
    }

  4. Guyz wright some example. Thise code have no information for piople whp dont understend PHP

    switch ($current_category_id) {
    case 1:
    case 19:
    echo “banner”;
    break;
    default:
    echo “whatever”;
    break;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *