Post by JNeuhoff » Fri Aug 07, 2009 1:01 am

There is a bug in file /catalog/model/catalog/product.php, in function getTotalProductsByCategoryId. It is supposed to return the number of products for a given category-id. However, sometimes it returns a higher count than actually available. I suggest the following bugfix:

Code: Select all

	public function getTotalProductsByCategoryId($category_id = 0) {
		$sql = "SELECT DISTINCT p2c.product_id FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2c.category_id = '" . (int)$category_id . "'";
		$query = $this->db->query( $sql );
		return count($query->rows);
	}

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member
Online

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by robbercrombie » Sun Aug 09, 2009 1:44 am

This is great, thank you

~ Robbercrombie


User avatar
New member

Posts

Joined
Mon Jul 20, 2009 5:05 pm


Post by Daniel » Sun Aug 09, 2009 1:54 am

can u post a link to an example of this bug?

OpenCart®
Project Owner & Developer.


User avatar
Administrator

Posts

Joined
Fri Nov 03, 2006 6:57 pm

Post by Bern » Sun Aug 09, 2009 2:04 am

A more efficient approach would be to get the DB to count the number of rows for you:

$sql = "SELECT COUNT(*) AS TOTAL FROM (SELECT DISTINCT p2c.product_id FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2c.category_id = '" . (int)$category_id . "') AS FOO";

I'm not a MySQL/PHP expert so I'm unsure about extracting the value from that single row instead of "return count($query->rows);"

Newbie

Posts

Joined
Sat Aug 08, 2009 9:45 pm

Post by Daniel » Sun Aug 09, 2009 2:08 am

I should be able to solve it by putting the product table at the front.

OpenCart®
Project Owner & Developer.


User avatar
Administrator

Posts

Joined
Fri Nov 03, 2006 6:57 pm
Who is online

Users browsing this forum: No registered users and 19 guests