Page 1 of 2
My category list with subs.
Posted: Sat Nov 17, 2007 2:08 pm
by tmcguire
I've managed to produce a modification to OpenCart which will list out your entire category list and indent the sub categories. I would like to share this but do not know the proper etiquette. Mainly because Luvs2drv has produced a commercial solution which appears to behave the same way my mod does.
I have not seen how the mod which Luvs2drv is integrated into OpenCart but I know that mine is a simple replacement of a block of code and a single commented out line in default.css.
So rather than piss individual people off I thought I would leave it up to the community to decide if they believe that there can be room for more than one mod which competes with a commercial product or if I should keep it to my self and wait for it to be a standard feature in a future release of OpenCart.
I have attached a screenshot of the navigation.
A side note. You’ll notice that the prices on the products are weird prices and not simple prices like $9.00 or $10.00. For some reason tax or something else that I don’t know is being added to the price and shown to guest viewers. I have no idea what is causing it. Any help with that would be great.
Re: My category list with subs.
Posted: Sat Nov 17, 2007 7:59 pm
by qfreddie
Hi
In my oppinion you can post your modification here, since it is an opensource shopcart CMS. Since it is open, and you made the modifications to the code, YOU deceide if you sell it or share it
Respect!
Re: My category list with subs.
Posted: Sat Nov 17, 2007 10:37 pm
by ogun
commercial mods for a fully mature and established cart/catalog are fine but this early in opencart's development, they're not going to do it much good at all if they're hampering the implementation of basic functionality. when opencart becomes more popular then there will be plenty of money to be made in templates, customisation contracts and specialised extensions/mods.
there's no reason why you shouldn't release something you've written for free (whatever it is) - commercial products can always compete by offering more functionality, and some users prefer to pay for a product on the understanding that it is kept up to date (open source projects can be abandoned, leaving the users high and dry until another developer picks up the torch) and more secure (in that it's not readily available to any would-be hacker with a copy of the opencart source).
that aside, i was going to work on an extended category menu this weekend based on the code in this thread:
http://forum.opencart.com/index.php?topic=244.0
how does yours work (if you don't mind posting the code)? what i was trying for was a single query, rather than multiple hits to the database.
Re: My category list with subs.
Posted: Sat Nov 17, 2007 10:43 pm
by tmcguire
Sounds like a strong enough argument to me. I'll prepare it and post it shortly.
-Tom
Re: My category list with subs.
Posted: Sat Nov 17, 2007 11:04 pm
by tmcguire
Ok here is my mod to show sub categories.
My goal was to display all of the categories from the database and when a sub category was encountered indent it.
My first clue how to do this was by looking at the code for the category selection when adding a product. (I always look for clues on how to accomplish something by examining other parts of any script.)
The mod does exactly what I want it to do so it may not be what you want. Please keep that in mind. For example I know my categories will never exceed 3 levels deep so for that reason I only built in 3 levels of indenting. If you are giving this mod to someone and you won't be managing the cart then you may want to add more 'else if' statements just to cover your bases.
Remember also that the greater the number of sub categories the wider you categories column will need to be. And changing the column width can mess with your content width.
I had one case where my category title was wrapping to the line below and making it very ugly. To correct this I went into phpmyadmin and changed the category name limit from 32 to 64 characters. This way I could put in several into the title and it would push the wrapped words to the right.
To apply this mod you will only need to edit 2 files.
/catalog/extension/module/category.php
/catalog/template/default/css/default.css
**** BACKUP THESE FILES FIRST ****
We will start with the default.css file.
Open default.css and search for ".box .category a"
Then comment out the first line which should be the background image.
Done.
Next open category.php
Select This:
Code: Select all
$results = $database->getRows("select c.category_id, cd.name from category c left join category_description cd on (c.category_id = cd.category_id) where parent_id = '0' and language_id = '" . (int)$language->getId() . "' order by c.sort_order");
foreach ($results as $result) {
$category_data[] = array(
'name' => $result['name'],
'href' => $url->href('category', false, array('path' => $result['category_id']))
);
Replace with:
Code: Select all
// tmcguire changed to show sub categories
$results = $database->getRows("select c.category_id, cd.name, c.parent_id, c.path from category c left join category_description cd on (c.category_id = cd.category_id) where cd.language_id = '" . (int)$language->getId() . "' order by c.path");
foreach ($results as $result) {
$getPath = count(explode('_', $result['path']))- 1;
if ($getPath == "0") {
$indent = "<img src=/shop/catalog/template/knitport01/image/bullet_1.png> ";
} else if ($getPath == "1") {
$indent = " <img src=/shop/catalog/template/knitport01/image/bullet_1.png> ";
} else if ($getPath == "2") {
$indent = " <img src=/shop/catalog/template/knitport01/image/bullet_1.png> ";
} else {
$indent = " <img src=/shop/catalog/template/knitport01/image/bullet_1.png> ";
}
$category_data[] = array(
'name' => $indent .''. $result['name'] .'',
'href' => $url->href('category', false, array('path' => $result['category_id']))
);
Save and upload your files.
That's pretty much it.
I would strongly recommend that you place a comment in any file where you edit that is outside of your template folder so that when the next Opencart upgrade comes out you can quickly locate what changes you have made so you can re-implement them.
In closing, I would like to say again that I built this for my own needs and it may not be as graceful as other commercial products but it get's the job done.
Good Luck,
Tom
Re: My category list with subs.
Posted: Sat Nov 17, 2007 11:26 pm
by qfreddie
Thanks man! It works! You just made my day! Finally i can upload to server
All the best
Re: My category list with subs.
Posted: Mon Nov 19, 2007 6:59 am
by Bebe
Fabulous! Your mod is much appreciated! Thank you for and sharing!
Re: My category list with subs.
Posted: Tue Nov 20, 2007 5:19 am
by aonekiz
Thanks for this mod. It works fine. thank you very much...
And I forgot, but I many thank Daniel for his fabulous open cart solution. A little jewel for me...
See you.
(ps : excuse me for my poor english..

)
Re: My category list with subs.
Posted: Tue Nov 20, 2007 9:42 pm
by ironman
thank you very much for sharing this mod.
Re: My category list with subs.
Posted: Sun Nov 25, 2007 10:36 pm
by 3806
Mod "Category list with subs" does not work.
After change of files subcategories are not visible.
(v0.7.7)
Re: My category list with subs.
Posted: Sun Nov 25, 2007 11:07 pm
by tmcguire
That's strange considering that everyone else has gotten it to work.
Were you working on previously modified versions of category.php?
Try copying the contents from the previous post into notepad and then copy from notepad into your category.php file.
What PHP editor are you using?
-Tom
Re: My category list with subs.
Posted: Mon Nov 26, 2007 7:41 pm
by 3806
I have been surprised by that works for all this, and at me was not present.
Re: My category list with subs.
Posted: Mon Nov 26, 2007 10:37 pm
by tmcguire
I would like to try to help you. Can you post a link to your cart so I can see what is wrong?
-Tom
Re: My category list with subs.
Posted: Tue Nov 27, 2007 12:24 am
by 3806
Thank you wery much, first part of answer is this:
http://forum.opencart.com/index.php?topic=622.0
It works!
Вначале надо создать подгруппы в админпанели, а потом сделать изменения в файлах, как написал камрад tmcguire
changed file: /catalog/template/default/css/default.css
Code: Select all
/* box category */
.box .category a {
/* background: url('../image/bullet_1.png') no-repeat 15px 5px; */
padding: 0px 0px 0px 25px;
display: block;
text-decoration: none;
margin-bottom: 8px;
}
changed file: /catalog/extension/module/category.php
Code: Select all
<?php
class ModuleCategory extends Controller {
function fetch() {
$config =& $this->locator->get('config');
$database =& $this->locator->get('database');
$language =& $this->locator->get('language');
$url =& $this->locator->get('url');
if ($config->get('category_status')) {
$language->load('extension/module/category.php');
$view = $this->locator->create('template');
$view->set('heading_title', $language->get('heading_title'));
$category_data = array();
// tmcguire changed to show sub categories
$results = $database->getRows("select c.category_id, cd.name, c.parent_id, c.path from category c left join category_description cd on (c.category_id = cd.category_id) where cd.language_id = '" . (int)$language->getId() . "' order by c.path");
foreach ($results as $result) {
$getPath = count(explode('_', $result['path']))- 1;
if ($getPath == "0") {
$indent = "<img src =store/catalog/template/default/image/bullet_1.png> ";
} else if ($getPath == "1") {
$indent = " <img src=store/catalog/template/default/image/bullet_1.png> ";
} else if ($getPath == "2") {
$indent = " <img src=store/catalog/template/default/image/bullet_1.png> ";
} else {
$indent = " <img src=store/catalog/template/default/image/bullet_1.png> ";
}
$category_data[] = array(
'name' => $indent .''. $result['name'] .'',
'href' => $url->href('category', false, array('path' => $result['category_id']))
);
}
$view->set('categories', $category_data);
return $view->fetch('module/category.tpl');
}
}
}
?>
And where is my little pictures "bullet_1.png" ?

Re: My category list with subs.
Posted: Tue Nov 27, 2007 10:51 pm
by tmcguire
You should check the path for the bullet image. Are you using a different template other than 'default'?
-Tom
Re: My category list with subs.
Posted: Wed Nov 28, 2007 11:46 pm
by nowhinjing
Hi,
I have taken the liberty of modifying your code Tom, I hope you don't mind.
In my slightly changed method you need to comment out the first two lines of .box .category a thus :
Code: Select all
.box .category a {
/*background: url('../image/bullet_1.png') no-repeat 15px 5px;
padding: 0px 0px 0px 10px;*/
display: block;
text-decoration: none;
margin-bottom: 8px;
}
and then replace exactly the same code with these lines :
Code: Select all
// tmcguire changed to show sub categories
$results = $database->getRows("select c.category_id, cd.name, c.parent_id, c.path from category c left join category_description cd on (c.category_id = cd.category_id) where cd.language_id = '" . (int)$language->getId() . "' order by c.path");
foreach ($results as $result) {
$getPath = min(count(explode('_', $result['path'])), 4);
$indent = "<img src='catalog/template/default/image/bullet_2.png' style='padding-left:" . $getPath*10 . "px;padding-right:7px;'>";
$category_data[] = array(
'name' => $indent .''. $result['name'] .'',
'href' => $url->href('category', false, array('path' => $result['category_id']))
);
}
you may need to play with it to get it right for your application :
the 4 in
Code: Select all
$getPath = min(count(explode('_', $result['path'])), 4);
gives the maximum indent.
the padding left / right items control left and right spacing.
@3806 you may find altering your image path to the one shown above helps
NWJ
Re: My category list with subs.
Posted: Sun Dec 02, 2007 1:22 am
by brightidea
i've been playing to try and make it only show the sub categories of the parent category, this is by no means finished, it was just to put it out there as an idea.
I believe zencart has a function doing something similar, might be worth an investigation.
Code: Select all
// tmcguire changed to show sub categories
// alan changed to only show the sub-categories of the parent category you are in
$results = $database->getRows("select c.category_id, cd.name, c.parent_id, c.path from category c left join category_description cd on (c.category_id = cd.category_id) where cd.language_id = '" . (int)$language->getId() . "' order by c.path");
foreach ($results as $result) {
if ((!$_GET["controller"] == "category") OR ($result['parent_id'] == 0) OR (substr($result['path'], 0, 1) == substr($_GET['path'], 0, 1)) OR ($result['parent_id'] == substr($_GET['path'], 0, 1)))
{
$getPath = min(count(explode('_', $result['path'])), 4);
$indent = "<img src='catalog/template/default/image/bullet_2.png' style='padding-left:" . $getPath*10 . "px;padding-right:7px;'>";
$category_data[] = array(
'name' => $indent .''. $result['name'] .'',
'href' => $url->href('category', false, array('path' => $result['category_id']))
);
}
}
Thanks,
Alan
Re: My category list with subs.
Posted: Sat Dec 15, 2007 2:42 am
by 3806
nowhinjing, thank you, it works, but not excellent.
The order of categories in Adminarea and on Store pages is broken: the fourth Category (under number) can appear on the seventh place on store Homepage.
Admin loses an opportunity to sort Сategories in the necessary order.
Заработало

Но криво. Нарушается порядок категорий в админпанели и на страницах магазина. Четвертая по номеру категория в админке может быть седьмой на странице магазина.
Вариант камрада
brightidea работает некорректно: при переходе на главную страницу выдается Undefined index в строке:
Code: Select all
if ((!$_GET["controller"] == "category") OR ($result['parent_id'] == 0) OR (substr($result['path'],
Re: My category list with subs.
Posted: Sun Jan 06, 2008 12:15 pm
by bruce
Hi all,
First of all thanks. I found the discussion on this topic and the examples very helpful.
With regard to the category sort order, have a look here
http://forum.opencart.com/index.php/topic,781.0.html for a fix to a similar problem that could be applied here too.
here is another variation of previous posts on the indenting that
- does not limit levels
- does not require a change the style sheet
- keeps the style modification entirely within the template.
category.php is modified as follows, retaining the original query by tmcguire
Code: Select all
//$results = $database->getRows("select c.category_id, cd.name from category c left join category_description cd on (c.category_id = cd.category_id) where parent_id = '0' and language_id = '" . (int)$language->getId() . "' order by c.sort_order");
$results = $database->getRows("select c.category_id, cd.name, c.parent_id, c.path from category c left join category_description cd on (c.category_id = cd.category_id) where cd.language_id = '" . (int)$language->getId() . "' order by c.path");
foreach ($results as $result) {
$category_data[] = array(
'name' => $result['name'],
'href' => $url->href('category', false, array('path' => $result['category_id'])),
'indent' => (count(explode('_', $result['path'])) - 1)
);
}
and category.tpl becomes
Code: Select all
<div class="box">
<div class="heading"><?php echo $heading_title; ?></div>
<div class="category" >
<?php foreach ($categories as $category) { ?>
<div style="padding-left: <?php echo 10 * $category['indent']; ?>px">
<a href="<?php echo $category['href']; ?>" ><?php echo $category['name']; ?></a>
</div>
<?php } ?>
</div>
</div>
The work is done by the extra div tag surrounding the anchor tag.
The end result in this example is an additional 10 pixel indent at each sub-category level.
cheers
Bruce
Re: My category list with subs.
Posted: Sun Jan 06, 2008 1:05 pm
by bruce
My final installment on this is an example where there is a style defined for each Category level in default.css.
category.php is unchanged from my previous post.
category.tpl is as follows
Code: Select all
<div class="box">
<div class="heading"><?php echo $heading_title; ?></div>
<div class="category" >
<?php foreach ($categories as $category) { ?>
<div class="level_<?php echo $category['indent']; ?>" >
<a href="<?php echo $category['href']; ?>" ><?php echo $category['name']; ?></a>
</div>
<?php } ?>
</div>
</div>
default.css will contain something like the following
Code: Select all
/* box category */
.box .category {
padding-top: 5px;
}
.box .category a {
padding: 0px 0px 0px 25px;
display: block;
text-decoration: none;
margin-bottom: 8px;
}
.box .category .level_0 a {
font-weight: bold;
/*background: url('../image/bullet_0.png') no-repeat 15px 5px; */
margin-left: 0px;
}
.box .category .level_1 a {
background: url('../image/bullet_1.png') no-repeat 15px 5px;
margin-left: 20px;
}
.box .category .level_2 a {
background: url('../image/bullet_2.png') no-repeat 15px 5px;
margin-left: 30px;
}
A screen shot of the menu is attached. (my category order is incorrect as discussed in the previous post also)