Author Topic: Show number of products in a category.  (Read 26228 times)

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #15 on: November 02, 2012, 14:59:29 pm »
Hi bytelord,

Thanks its started to work but its returning (0) products for all the subcats whereby there are products under them Help please. Thanks

bytelord

  • Nikos
  • Global Moderator
  • Hero Member
  • *
  • Posts: 2369
Re: Show number of products in a category.
« Reply #16 on: November 02, 2012, 15:24:24 pm »
any live url?
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #17 on: November 02, 2012, 15:40:13 pm »
Hi Bytelord

Thanks I have just figured it out that it is working but the count is not right. You can see it at http://www.myghanashop.com/index.php/4/cars

See BMW says 8 products but its actually 9. Is it counting the products by vendor? Also the food category has 1 product under Fruits and vegetables but it says 0. This is at http://www.myghanashop.com/index.php/3/food

Thanks


bytelord

  • Nikos
  • Global Moderator
  • Hero Member
  • *
  • Posts: 2369
Re: Show number of products in a category.
« Reply #18 on: November 02, 2012, 15:53:37 pm »
hello,

works for me ... you are using a commercial template but should work also, i think is not counted products with 0price ... am i right?

Regards
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #19 on: November 02, 2012, 15:57:17 pm »
Thanks for figuring that out but am not sure. Will check and get back to you. Thanks

bytelord

  • Nikos
  • Global Moderator
  • Hero Member
  • *
  • Posts: 2369
Re: Show number of products in a category.
« Reply #20 on: November 02, 2012, 15:59:45 pm »
may be that products assigned to other subcategory?

The countProducts function works fine .. do you use multivendor?

public function countProducts($cat_id=0) {

      if(!empty($this->_db))$this->_db = JFactory::getDBO();
      $vendorId = 1;
      if ($cat_id > 0) {
         $q = 'SELECT count(#__virtuemart_products.virtuemart_product_id) AS total
         FROM `#__virtuemart_products`, `#__virtuemart_product_categories`
         WHERE `#__virtuemart_products`.`virtuemart_vendor_id` = "'.(int)$vendorId.'"
         AND `#__virtuemart_product_categories`.`virtuemart_category_id` = '.(int)$cat_id.'
         AND `#__virtuemart_products`.`virtuemart_product_id` = `#__virtuemart_product_categories`.`virtuemart_product_id`
         AND `#__virtuemart_products`.`published` = "1" ';
         $this->_db->setQuery($q);
         $count = $this->_db->loadResult();
      } else $count=0 ;

      return $count;
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #21 on: November 02, 2012, 16:17:33 pm »
Yes I use multivendor called vm vendor. What should I do with that code and where should I put it? Also am not sure why my product prices aren't showing at all. Any extra help please?

bytelord

  • Nikos
  • Global Moderator
  • Hero Member
  • *
  • Posts: 2369
Re: Show number of products in a category.
« Reply #22 on: November 02, 2012, 16:57:53 pm »
hm,

You could change that function query load to select multivendor ... but i am afraid maybe other issues will be caused ... so i am not aware at the moment what issues could that create ...

try to play around with the following function that is located under joomla_folder\administrator\components\com_virtuemart\helpers\shopfunctions.php ... but this is a core hack and i am not suggested to change it because may be have side effects and your shop does not work properly after. I think that solution works only for one vendor at the moment and multivendor is under development.

Code: [Select]
public function countProducts($cat_id=0) {

      if(!empty($this->_db))$this->_db = JFactory::getDBO();
      $vendorId = 1;
      if ($cat_id > 0) {
         $q = 'SELECT count(#__virtuemart_products.virtuemart_product_id) AS total
         FROM `#__virtuemart_products`, `#__virtuemart_product_categories`
         WHERE `#__virtuemart_products`.`virtuemart_vendor_id` = "'.(int)$vendorId.'"
         AND `#__virtuemart_product_categories`.`virtuemart_category_id` = '.(int)$cat_id.'
         AND `#__virtuemart_products`.`virtuemart_product_id` = `#__virtuemart_product_categories`.`virtuemart_product_id`
         AND `#__virtuemart_products`.`published` = "1" ';
         $this->_db->setQuery($q);
         $count = $this->_db->loadResult();
      } else $count=0 ;

      return $count;
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #23 on: November 04, 2012, 00:18:57 am »
Hi bytelord,
Many thanks for your generous support. I have used an alternative but you have opened my eyes more. Thanks

bytelord

  • Nikos
  • Global Moderator
  • Hero Member
  • *
  • Posts: 2369
Re: Show number of products in a category.
« Reply #24 on: November 04, 2012, 01:25:01 am »
Hello,

Great you find a solution, you could share if you like.

Regards
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #25 on: November 04, 2012, 02:32:43 am »
Hi,
Just like you said, your solution works only for a single vendor and that is the super user, which suggests that vm only counts for products added by the super user and not other users. In order for other users products to be counted, I had to make every vendor a super user which will be stupid for me to do so. I have rather used a module which displays the categories in list form and which counts every product individually by category. I am happy with it so far. Saying so, I will be happy to revert back to the original if an update or a hack is available. Many thanks for your efforts.

bytelord

  • Nikos
  • Global Moderator
  • Hero Member
  • *
  • Posts: 2369
Re: Show number of products in a category.
« Reply #26 on: November 04, 2012, 03:23:03 am »
Hello,

You could try to add a new function similar to the countProducts inside category model for all vendors to use it. Inside the file joomla_folder\administrator\components\com_virtuemart\models\category.php around line 375 after the coundProducts function (no matter where but easier to locate it). So please try the add following code inside models\category.php:


Code: [Select]
/**
* count the products in a category for all Vendors
*
* @author RolandD, Max Milbers, Bytelord
* @return array list of categories product is in for all vendors
*/
public function countAllVendorsProducts($cat_id=0) {

if(!empty($this->_db))$this->_db = JFactory::getDBO();
if ($cat_id > 0) {
$q = 'SELECT count(#__virtuemart_products.virtuemart_product_id) AS total
FROM `#__virtuemart_products`, `#__virtuemart_product_categories`
WHERE `#__virtuemart_product_categories`.`virtuemart_category_id` = '.(int)$cat_id.'
AND `#__virtuemart_products`.`virtuemart_product_id` = `#__virtuemart_product_categories`.`virtuemart_product_id`
AND `#__virtuemart_products`.`published` = "1" ';
$this->_db->setQuery($q);
$count = $this->_db->loadResult();
} else $count=0 ;

return $count;
}

After that inside your category default template you will use the following code:

 
Code: [Select]
<?php $categoryModelVmModel::getModel('category');
           echo 
'('.$categoryModel->countAllVendorsProducts($category->virtuemart_category_id ).')'?>

I haven't test it but you could test and please report if is working...

Regards
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #27 on: November 05, 2012, 01:00:19 am »
Hi bytelord,
You have solved my biggest problem. Your solution worked very good. You can check on my site. Every product is counted by your hack and it is just how I wanted it. I don't know how to thank you. You are very good and helpful. Wish everyone was like you. I did a template override as well so hopefully no update will mess it up. Thank you very much!!!

bytelord

  • Nikos
  • Global Moderator
  • Hero Member
  • *
  • Posts: 2369
Re: Show number of products in a category.
« Reply #28 on: November 05, 2012, 02:32:20 am »
Glad i helped :)

Regards
Production: Joomla 2.5.8 | VM 2.0.14 | PHP 5.3.13
Testing     : Joomla 2.5.8 | VM 2.0.16 | PHP 5.3.8
Testing     : Joomla 2.5.8 |    VM 2.1   | PHP 5.3.8

- Don't Forget to mark thread as solved when it is solved!
- Please do not PM with support questions, use the forum!

kanbosk

  • Beginner
  • *
  • Posts: 12
Re: Show number of products in a category.
« Reply #29 on: November 19, 2012, 01:03:41 am »
Hi bytelord,
Sorry to bother you again but I thought I did a template override, not sure what happened but the new VM update messed the hack up so its not working anymore.  I know am asking too much but if you have some spare time, can you please look into the hack to show all vendor products count in VM update 2.0.14. Thanks