Community Forums

Categories and Subcategories

Coding & concept discussion for OpenCart v1.x development

Categories and Subcategories

Postby Franz-Peter » Sun Sep 13, 2009 5:22 pm

It would be very nice, if there would be a possibility to have new products in categories or subcategories. Actually if there are some root categories with some subcategories a customer has to click through a lot of categories to see products. The only place actually to see products is on the home page.
A good candidate for such a function would be the new products part. It would be very nice if that part could display in every category (from root to subcategory) a few new products.
An example: There is a root category with printers. The root category has subcategories: Laser, Inkjet. Those subcategories have other subcategories like Color Laser, Mono Laser, Laser Multifunction, Inkjet, Inkjet Multifunction a.s.o. Until the third subcategory a customer does not see even one product, just links to other categories and a few category pictures. So a customer needs to click into root category, subcategory1, subcategory 2 a.s.o. to see a few products. that is not very interesting for customers.
Franz-Peter
 
Posts: 74
Joined: Tue Aug 25, 2009 8:30 am

Re: Categories and Subcategories

Postby Qphoria » Sun Sep 13, 2009 5:35 pm

You mean you want a new products block to be visible on more pages, not just home page?
Image Image
Donate!|OpenCart Basics|GeoZones
Help me get more development cloud storage - Click Here to get DropBox
User avatar
Qphoria
Administrator
 
Posts: 18209
Joined: Mon Jul 21, 2008 7:02 pm
Donate to Qphoria

Re: Categories and Subcategories

Postby JNeuhoff » Sun Sep 13, 2009 9:41 pm

There is nothing to prevent you from making a product belong to more than one category, e.g. it couyld belong to a whole category chain.
J.Neuhoff - MHC Web Design

OpenCart Override Engine (Version 5.3)
allowing addons to override and modify core methods, language files and templates (see also FAQ)
User avatar
JNeuhoff
 
Posts: 2113
Joined: Tue Dec 04, 2007 7:38 pm

Re: Categories and Subcategories

Postby Franz-Peter » Mon Sep 14, 2009 4:52 pm

Dear J. Neuhoff,
it is true to make a product visible in more than one category. this happens by clicking in the admin under products and activating some categories. The problem is that if you have a lot of daily changing products with a lot of categories and subcategories, you make take more than one day to do so. And if the daily product import (automated by a cron script, which pulls those data from a distributor) of about 40 000 products with a lot of categories and subcategories it takes the whole day to click around in the admin area.

If the new products would be a content module with some more logic, it could be integrated with an <?php include (....); ?> and would be able to show new products in categories. Actually it is not a module or block, it is just a part of the home.php.

And it is not very customer friendly to show only categories and subcategories a.s.o. without showing a few products (new ones for example).
OpenCart is a new shopping cart which should be a little bit better than older solutions like oscommerce. But for that case oscommerce does a much better job and that is only because the new products is implemented as a module and not as part of the home.php code.
Franz-Peter
 
Posts: 74
Joined: Tue Aug 25, 2009 8:30 am

Re: Categories and Subcategories

Postby Franz-Peter » Mon Sep 14, 2009 5:03 pm

Dear Qphoria,

your question to my question is exactly the thing I mean. There should be a block module for new products, which could be integrated in other than home page (oscommerce is an example, it is very old but the new products module there operates very nice).

We loose a lot of promotion possibilities if me do not show products to the customers. From the customer point of view OpenCart does not invite to add some other products to the cart. the customer has to click from root category through a lot of subcategories before he may see products.
Franz-Peter
 
Posts: 74
Joined: Tue Aug 25, 2009 8:30 am

Re: Categories and Subcategories

Postby cowpoke McStink » Mon Sep 14, 2009 8:41 pm

Not to threadjack, but I thought it was close enough...

I would be thrilled in the category.tpl would show the sub categories at the top as it does now, and the second table showed not only products within the current category, but all applicable sub categories as well.

This functionality would basically make the sub-catagories more like filters than a directory structure, which I think is the more common approach in today's e-commerce.

I guess I'm going to have to edit the catagory model "getProductsByCategoryId" to find the category's child categories and pull those products as well. A recursive function of something.. Has anyone already done this?

This approach just seems to make more sense then having to apply several category selections when entering the product.
User avatar
cowpoke McStink
 
Posts: 14
Joined: Thu Aug 13, 2009 10:01 pm
Location: The Vast Deserts of California

Re: Categories and Subcategories

Postby Qphoria » Mon Sep 14, 2009 11:40 pm

cowpoke McStink wrote: I would be thrilled in the category.tpl would show the sub categories at the top as it does now, and the second table showed not only products within the current category, but all applicable sub categories as well.

Doesn't it already do this? Or you mean you want ALL sub-subs-subs to show on this page?

Like:
-------------------------------------
| direct subs of this category |
++++++++++++++++++++++++++++
| Products in this category |
+++++++++++++++++++++++++++
| subs of sub-categories |
---------------------------------------
Image Image
Donate!|OpenCart Basics|GeoZones
Help me get more development cloud storage - Click Here to get DropBox
User avatar
Qphoria
Administrator
 
Posts: 18209
Joined: Mon Jul 21, 2008 7:02 pm
Donate to Qphoria

Re: Categories and Subcategories

Postby cowpoke McStink » Tue Sep 15, 2009 4:16 am

Unfortunately, It doesn't..

I mean like this:
-------------------------------------
| direct subs of this category |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| Products in this category and products in all sub cats and sub sub cats....|
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I want a listing of every product in the current directory and all products within any sub category of the current directory.

For instance, take the following scenario..
A. Animals (main category)
1. Cats (Sub Category)
a. male (Sub Sub Category)
- Garfield (Product)
- Simba
b. female
- Nermal
2. Dogs
a. male
- Santas Little Helper
- Cujo
b. female
- bitch 1
- bitch 2
3. Goats
a. Milking
- LadyGoat1
b. Non-Milking
-Billy


If this is our category/product listing and I navigate to the Animals category I will get a listing of sub categories only.. I think the customer should see both the sub categories, and all animals (paginated if necessary)

- Garfield (Product)
- Simba
- Nermal
- Santas Little Helper
- Cujo
- bitch 1
- bitch 2
- LadyGoat1
-Billy

If the customer drills down to the Dogs sub category.. they should see the male and female sub categories AND all products within those directories..

- Santas Little Helper
- Cujo
- bitch 1
- bitch 2

I think this makes sense... but hey, maybe I'm just crazy! I think that the requirement of the admin to manually add the specific and all parent categories is contrary to the easy to use tone of the rest of the framework.

But I'm not bitching.... I'm a fixer not a complainer, so after I take the time to write some horrible looking code that gets the job done, I'll post it here for the real pros to clean up.

-Cowpoke
User avatar
cowpoke McStink
 
Posts: 14
Joined: Thu Aug 13, 2009 10:01 pm
Location: The Vast Deserts of California

Re: Categories and Subcategories

Postby cowpoke McStink » Tue Sep 15, 2009 10:36 pm

Alright,

True to my word I am back with some code that changes the category view to show all products within that category, and ALL sub categories to that category.

Here is what I did:

Add the following functions within the catalog\model\product.php file:
Code: Select all
public function buildWhereClause($parent_id = 0) {
      $results = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->language->getId() . "' ORDER BY c.sort_order");
      $num_rows = count($results->rows);
      If ($num_rows != 0) {
      foreach ($results->rows as $result) {
      $this->where_clause .= "OR p.status = '1' AND p.date_available <= NOW() AND p2c.category_id = '" . (int)$result['category_id'] . "' ";
      $this->buildWhereClause($result['category_id']);
      }
   }
   }
   public function getTotalProductsByCategoryIdwithSubs($category_id = 0) {
      $this->buildWhereClause($category_id);
      $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2c.category_id = '" . (int)$category_id . "'" . $this->where_clause);
      return $query->row['total'];
   }


Then open up catalog\model\catalog\category.php and add the following function:
Code: Select all
   public function getCategoriesandSubs($parent_id = 0) {
      static $cat_branch = array();
      $results = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->language->getId() . "' ORDER BY c.sort_order");
      $num_rows = count($results->rows);
      If ($numcata_rows != 0) {
         foreach ($results->rows as $result) {
            $cat_branch[] = $result['category_id'];
            $this->getCategoriesandSubs($result['category_id']);
         }
      }
      $num_subcats = count($cat_branch);
      return $cat_branch;
   }


Finally, make the following changes to you catalog\controller\product\category.php file:

Around line 91, change the function name that fetches the $product_total like so:
Code: Select all
$product_total = $this->model_catalog_product->getTotalProductsByCategoryIdwithSubs($category_id);


Just below that after the data['products'] is declared around line 113 add the following:
Code: Select all
            $sub_cats = $this->model_catalog_category->getCategoriesandSubs($category_id);
            array_unshift($sub_cats, $category_id);
              foreach ($sub_cats as $category_id) {


Finally, close up the foreach loop we added by adding the appropriate } after the data['products'] array is filled in and before the "config customer price" stuff kicks in.

I think that is it. I'm using version 1.3.2

If I have left anything out, go ahead and yell at me. If your utter lack of PHP experience makes this modification too difficult for you, don't yell at me, just go get more experience.

-Cowpoke
User avatar
cowpoke McStink
 
Posts: 14
Joined: Thu Aug 13, 2009 10:01 pm
Location: The Vast Deserts of California

Re: Categories and Subcategories

Postby cowpoke McStink » Wed Sep 16, 2009 2:53 pm

I made a mistake in copying one of the snippets above.

The catalog\model\catalog\category.php file function should not have the extra "cata" in the middle of my $num_rows variable. Use this instead..

Code: Select all
   public function getCategoriesandSubs($parent_id = 0) {
      static $cat_branch = array();
      $results = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->language->getId() . "' ORDER BY c.sort_order");
      $num_rows = count($results->rows);
      If ($num_rows != 0) {
         foreach ($results->rows as $result) {
            $cat_branch[] = $result['category_id'];
            $this->getCategoriesandSubs($result['category_id']);
         }
      }
      $num_subcats = count($cat_branch);
      return $cat_branch;
   }
User avatar
cowpoke McStink
 
Posts: 14
Joined: Thu Aug 13, 2009 10:01 pm
Location: The Vast Deserts of California

Re: Categories and Subcategories

Postby victor.vhv » Thu Jun 17, 2010 3:46 pm

Thanks! This worked for me :) I was going to write it myself, but saw it here and couldn't avoid some C&P, to make it work on 1.4.8, I had to change the way it references to the language:

Change from "(int)$this->language->getId()" to "(int)$this->config->get('config_language_id')" everywhere it's referenced.

Greetings,
Victor
victor.vhv
 
Posts: 1
Joined: Thu Jun 17, 2010 3:43 pm

Re: Categories and Subcategories

Postby travist6983 » Thu Apr 26, 2012 2:24 am

I am not sure if anyone will see this since the thread is so old but i think the code here is close to being able to help...

You can see what i want it to do here http://thepondshop.com/index.php?route= ... ry&path=36 but i want it to do this on all pages? if you hover over the category Pond and Lake Aerators it shows the sub categories but i want to be able to do this on all pages such as home, other categories, etc...

Any help would be greatly appreciated!

Thanks
travist6983
 
Posts: 30
Joined: Sat Jan 08, 2011 5:49 pm


Return to Concepts

Who is online

Users browsing this forum: No registered users and 7 guests

Hosted by Arvixe Web Hosting