Post by pyro-ns » Sun Oct 24, 2010 8:52 pm

Hi everyone,

Not sure how useful this is to other people, but I missed this little feature, coming from OsCommerce to OpenCart 1.4.9. Out of the box in OsCommerce, when a category has products in it, the number of products appears in brackets beside the category name in navigation. I've edited the source to make the same thing happen for OpenCart (1.4.9).

in catalog\model\catalog\category.php on line 16, there is this line of PHP:

Code: Select all

$category_data = $query->rows;
directly after that line, add this:

Code: Select all

// Get the count for the rows that have a count
$count = $this->db->query("SELECT COUNT(*) AS numproducts, p2c.category_id AS newcat FROM " . DB_PREFIX . "_product_to_category p2c GROUP BY p2c.category_id");
			
			$count_data = $count->rows;
			
			// Append the count to the categories that have a count.
			foreach($category_data as &$result){
				foreach($count_data as &$count_result){
					if($result['category_id'] == $count_result['newcat']){
						$result['count'] = $count_result['numproducts'];
					}
				}
			}
then in catalog\controller\module\category, around line 59 there is an if statement that starts with this line of PHP:

Code: Select all

if ($this->category_id == $result['category_id']) { 
you need to modify that if statement to the following:

Code: Select all

if ($this->category_id == $result['category_id']) {
				$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '"><b>' . $result['name'] . '</b></a>';
				if($result['count']){
					$output .= ' <b>(' . $result['count'] . ')</b>';
				}
			} else {
				$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path)  . '">' . $result['name'] . '</a>';
				if($result['count']){
					$output .= ' (' . $result['count'] . ')';
				}
			}
And now you have the number of products appearing on your sidebar as well. I hope that's of some use to someone else :)

Cheers,
Josh

p.s please bare with my tabbing in the examples, i can't seem to get them right on this forum :(

Newbie

Posts

Joined
Tue Oct 19, 2010 11:00 pm

Post by shmerler » Wed Feb 16, 2011 10:42 pm

hello josh ... nice idea ...

your second bit of code should be
// Get the count for the rows that have a count
$count = $this->db->query("SELECT COUNT(*) AS numproducts, p2c.category_id AS newcat FROM " . DB_PREFIX . "product_to_category p2c GROUP BY p2c.category_id");

.. was an underscore too many before product_to_category in the group by ...

but then i got another error and gave up ... cause i'm fairly clueless ... hope you can get it working :)

Newbie

Posts

Joined
Thu Sep 30, 2010 6:23 pm
Who is online

Users browsing this forum: No registered users and 2 guests