How to exclude some categories when listing categories in the header in OpenCart

In trying to introduce a bit more flexibility into OpenCart I’ve realised that category pages are great for special promotions or themed pages in OpenCart. However, you may not want these promotional pages or themed pages to show up in your main navigation. Here’s how to exclude some categories from the main navigation in your header in OpenCart.

As an example, at this time of year you might have some products that you want to promote for Christmas. You could use a Category page to create a beautiful Christmas page and list your Christmas products there. However, you may prefer to promote this page on your home page with a big splash image and link to it from there, rather than have it appear in your main navigation, which you may want to keep the same all year round.

This bit of code adapts the original code in my How to list categories in the header in OpenCart post. It starts by creating an array of the categories that you wish to exclude from the main navigation. I’ve used some of the original OpenCart default categories as examples. You will need to enter the names of the specific categories you do not want to list in the first line.

If you use more than one language, you’ll have to list all the translations of the category name. Sorry about that. Admittedly this could have been avoided if I had excluded categories by category id, but if you are using SEO URLs it is difficult to find out what the id of a category is without having a look at your database and I wanted to make this as easy to use as possible.

This is how it works. Before listing a category the code loops through the $excludes array. If the name of the category appears in the array then $listcat is set to false and the category is not listed. It’s as simple as that.

<?php
$excludes = array('Desktops', 'Components', 'Software');
$this->load->model('tool/seo_url');
$results = $this->model_catalog_category->getCategories();
if ($results) {$output = '<ul>';}
foreach ($results as $result) {
$listcat = true;
foreach ($excludes as $exclude) {
if ($result['name'] == $exclude) {$listcat = false;}
}
if ($listcat == true) {
$output .= '<li>';
$new_path = $result['category_id'];
$unrewritten = HTTP_SERVER.'index.php?route=product/category&path='.$new_path;
$rewritten = $this->model_tool_seo_url->rewrite($unrewritten);
$output .= '<a href="'.str_replace('&', '&amp;', $rewritten).'">'.$result['name'].'</a>';
$output .= '</li>';
}
}
if ($results) {$output .= '</ul>';}
echo $output;
?>

This entry was posted in OpenCart Mods. Bookmark the permalink.

8 Responses to How to exclude some categories when listing categories in the header in OpenCart

  1. jason says:

    Craig,
    I really like what you have done on your opencart site:-)
    By far it is the nicest and cleanest layout I’ve seen. I was just wondering if you would ever think about selling a copy of your theme? I would not use a mirror of it, but more of a base to skin over. If you are interested please contact me.

    Have a great day!
    Jason Estes
    idahostore@gmail.com

  2. Craig says:

    Hi Jason, do you mean the Scarlet and Jones theme? If so, I’m afraid it’s not for sale as I did it for the client exclusively. However, I will soon be releasing a free 1 column theme that is quite similar. So please keep checking my opencart themes at http://www.opencart.com/index.php?route=extension/extension&filter_username=Chones

  3. Heather says:

    Not being a shop owner, I never even thought to use a category page for a special/theme page. Nice!

    There is also a way to do this with just css. Using your same technique to output the categories where you have:
    $output .= '';

    I altered it to
    $output .= '';

    It takes some inspecting to see what the category id is, but then in your stylesheet you could do something like
    li.menu_cat_45 { display: none }

    Also handy for styling/highlighting certain categories differently!

  4. Heather says:

    Doh it ate some of my code. But I added a class definition to the in the output using class= menu_cat_’.$result["category_id"].’

  5. Craig says:

    Hi Heather, that’s a great idea. Thanks. Sorry about WordPress eating your code – I must find a code plug-in.

  6. John says:

    Hi Craig, I thought I would just take this opportunity to say thanks for your free theme nice cleaner by default. You have modified it with the customer in mindso it clean and uncluttered, the product page and checkout page are a great improvement on the default skin
    One thing i would ask, As I am selling only within Australia, I would like to remove the language and country drop downs from their position in the header and insert the search box. How can one accomplish that?
    Thanks
    John

  7. John says:

    Hi Craig, how did that photo of me get there?

  8. Craig says:

    Hi John, thanks very much for your comments. Glad you like the theme. If you look in the header.tpl file in template > common you will find a line:
    <div id="cmnavdiv">
    You can comment out the select boxes by adding <!-- just after that line, and then putting --> just before the line
    <!-- Navigation Buttons -->

    I’d leave the search box where it is – top right is where most people look. However, if you want to move it, you’ll also find the code for the search box in the header.tpl file under <!-- Search Box -->
    You can put that just after <div id="cmnavdiv">
    You’ll also need to fiddle with the styles in the stylesheet to make it fit.
    Yours,
    Craig
    P.S. You must have uploaded a photo to Gravatar – it’s drawn in from there based on your email address.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>