Display all products under the sub-categories of the current category too VM2

Started by vanaveno, February 11, 2012, 09:21:41 AM

Previous topic - Next topic

vanaveno

I need to display all products under the sub-categories of the current category too.
Example: Category tree is:
main-category1 (show all of products)
   -subcategory1 (only products subcategory -1)
   -subcategory2 (only products subcategory -2)
....
Now I get only menu of subcategories I not see products. Is it possible to change this I want to see products not menu subcategory.
I thing there is similar solution of this problem, but it is at VM 1.
http://forum.virtuemart.net/index.php?topic=20837.0
But I need it for VM2. Thanks for respond.

gamecat2300

Hi,

I as well would love a solution for this. I was able to get this to work very well with"
http://extensions.joomla.org/extensions/extension-specific/virtuemart-extensions/virtuemart-navigation/6775

But now that I've moved the site to VM 2 this is no longer an option. Any clues or tips on this?

=-s-=

chillmen


luke67


renevm


mysaq

So... I'm PHP beginner... but... this works for me... :)

After the setKeyWord function in \administrator\components\com_virtuemart\models\product.php place:


function untreeCat($vm_catid, &$ccont){
   $dbx = JFactory::getDBO();
   $q = 'SELECT `category_child_id` FROM `#__virtuemart_category_categories` WHERE `category_parent_id`="'.$vm_catid.'"';
   $dbx->setQuery($q);
   $dby = $dbx->query();       
   if (!$dby->num_rows) {
      return;
   } else {   
        while($tt = $dby->fetch_row()) {
           array_push($ccont, $tt[0]);
           $kat = $tt[0];
           $this->untreeCat($kat, $ccont);
   }
    }
}


Then search for this:


if ($virtuemart_category_id>0){
    $joinCategory = true ;
    $where[] = ' `#__virtuemart_product_categories`.`virtuemart_category_id` = '.$virtuemart_category_id;
}


... and replace it with following:


if ($virtuemart_category_id>0){
   $joinCategory = true ;
   $catscont = array();
   $this->untreeCat($virtuemart_category_id, $catscont);
   $qkat = ' `#__virtuemart_product_categories`.`virtuemart_category_id` IN('.$virtuemart_category_id;
    foreach ($catscont as &$kat){
       $qkat .= ', '.$kat;
    }
    $qkat .= ')';
    $where[] = $qkat;
}


EDIT: There was one missing row in the code here. So I replaced the last part by the Piszi's version. (tested and works)

Piszi

Actually I think you could make your code more compact if you concat your category id-s, than your query will result only one line or nothing.
So you could do a query like this:
$query = 'SELECT GROUP_CONCAT(`category_child_id` SEPARATOR ", ") FROM `#__virtuemart_category_categories` WHERE `category_parent_id`="'.$vm_catid.'"';
This will return something like this: 1, 2, 3, ...

Oh and your $where[] mod has a syntax error
If you put a comma in the query and you doesn't have a child category than you will get a MySQL error because of that comma.
So you should make your code like this:

$catscont = array();
       $this->untreeCat($virtuemart_category_id, $catscont);
       $qkat = ' `#__virtuemart_product_categories`.`virtuemart_category_id` IN('.$virtuemart_category_id;
       foreach ($catscont as &$kat){
           $qkat .= ', '.$kat;
       }
       $qkat .= ')';
       $where[] = $qkat;


Otherwise great work!

mysaq

I modified the VM1 version only. The syntax error is caused by one missing row in the code here. ( $qkat = str_replace(',)', ')', $qkat); ) Don't know why it diappeared...  ???

I replaced that part of code by your version. Thanks.

rupesh


Can you please send me product.php

on my site i'm getting
Notice: Trying to get property of non-object in E:\xampp\htdocs\elifeaftercheckout\administrator\components\com_virtuemart\models\product.php on line 172

its not working :'(
Thank you
Rupesh
Quote from: Piszi on June 20, 2012, 02:01:23 AM
Actually I think you could make your code more compact if you concat your category id-s, than your query will result only one line or nothing.
So you could do a query like this:
$query = 'SELECT GROUP_CONCAT(`category_child_id` SEPARATOR ", ") FROM `#__virtuemart_category_categories` WHERE `category_parent_id`="'.$vm_catid.'"';
This will return something like this: 1, 2, 3, ...

Oh and your $where[] mod has a syntax error
If you put a comma in the query and you doesn't have a child category than you will get a MySQL error because of that comma.
So you should make your code like this:

$catscont = array();
       $this->untreeCat($virtuemart_category_id, $catscont);
       $qkat = ' `#__virtuemart_product_categories`.`virtuemart_category_id` IN('.$virtuemart_category_id;
       foreach ($catscont as &$kat){
           $qkat .= ', '.$kat;
       }
       $qkat .= ')';
       $where[] = $qkat;


Otherwise great work!


perlarenee

i tried this fix as well and could see now change. I'm in vm 2.8 and joomla 2.5.6.

ThomasR

Still looking for this solution.. Does someone have found a way to get this done?

Robert V.

Quote from: vanaveno on February 11, 2012, 09:21:41 AM
I need to display all products under the sub-categories of the current category too.
Example: Category tree is:
main-category1 (show all of products)
   -subcategory1 (only products subcategory -1)
   -subcategory2 (only products subcategory -2)
....
Now I get only menu of subcategories I not see products. Is it possible to change this I want to see products not menu subcategory.
I thing there is similar solution of this problem, but it is at VM 1.
http://forum.virtuemart.net/index.php?topic=20837.0
But I need it for VM2. Thanks for respond.

Tested this under joomla 2.5.7 with VM 2.0.10.

Within Joomla if you create a menu item that will show every subcategory with the products choose VirtueMart Categories Layout as a link from that menu
for every subcategory make a submenu item that links to every single category layout.


Hence:  main-category1 (show all of the products) - link this one to VirtueMart Categories Layout -> This will display all the products of all categories.
                -subcategory1 (only products subcategory -1) - Link this one to VirtueMart » Category Layout (subcategory 1)
                -subcategory2 (only products subcategory -2) - Link this one to VirtueMart » Category Layout (subcategory 2)

Now depending on the amount of products you'll get a set of pages (default 10 products per page). Just enhance this number to display more products per page.

hope this helped.
                     

rupesh

Quote from: Robert V. on September 17, 2012, 01:43:52 AM
Quote from: vanaveno on February 11, 2012, 09:21:41 AM
I need to display all products under the sub-categories of the current category too.
Example: Category tree is:
main-category1 (show all of products)
   -subcategory1 (only products subcategory -1)
   -subcategory2 (only products subcategory -2)
....
Now I get only menu of subcategories I not see products. Is it possible to change this I want to see products not menu subcategory.
I thing there is similar solution of this problem, but it is at VM 1.
http://forum.virtuemart.net/index.php?topic=20837.0
But I need it for VM2. Thanks for respond.

Tested this under joomla 2.5.7 with VM 2.0.10.

Within Joomla if you create a menu item that will show every subcategory with the products choose VirtueMart Categories Layout as a link from that menu
for every subcategory make a submenu item that links to every single category layout.


Hence:  main-category1 (show all of the products) - link this one to VirtueMart Categories Layout -> This will display all the products of all categories.
                -subcategory1 (only products subcategory -1) - Link this one to VirtueMart » Category Layout (subcategory 1)
                -subcategory2 (only products subcategory -2) - Link this one to VirtueMart » Category Layout (subcategory 2)

Now depending on the amount of products you'll get a set of pages (default 10 products per page). Just enhance this number to display more products per page.

hope this helped.
                     

I know this solution! just ping me i will help you!

Doomas

Quote from: mysaq on June 18, 2012, 23:57:30 PM
So... I'm PHP beginner... but... this works for me... :)

After the setKeyWord function in \administrator\components\com_virtuemart\models\product.php place:


function untreeCat($vm_catid, &$ccont){
   $dbx = JFactory::getDBO();
   $q = 'SELECT `category_child_id` FROM `#__virtuemart_category_categories` WHERE `category_parent_id`="'.$vm_catid.'"';
   $dbx->setQuery($q);
   $dby = $dbx->query();       
   if (!$dby->num_rows) {
      return;
   } else {   
        while($tt = $dby->fetch_row()) {
           array_push($ccont, $tt[0]);
           $kat = $tt[0];
           $this->untreeCat($kat, $ccont);
   }
    }
}


Then search for this:


if ($virtuemart_category_id>0){
    $joinCategory = true ;
    $where[] = ' `#__virtuemart_product_categories`.`virtuemart_category_id` = '.$virtuemart_category_id;
}


... and replace it with following:


if ($virtuemart_category_id>0){
   $joinCategory = true ;
   $catscont = array();
   $this->untreeCat($virtuemart_category_id, $catscont);
   $qkat = ' `#__virtuemart_product_categories`.`virtuemart_category_id` IN('.$virtuemart_category_id;
    foreach ($catscont as &$kat){
       $qkat .= ', '.$kat;
    }
    $qkat .= ')';
    $where[] = $qkat;
}


EDIT: There was one missing row in the code here. So I replaced the last part by the Piszi's version. (tested and works)

Thanks for this solution, i made the following  changes to make this work in Joomla 2.5.7 and Virtumart 2.0.11d. 


function untreeCat($vm_catid, &$ccont){
$db = JFactory::getDBO();
$query = 'SELECT `category_child_id` FROM `#__virtuemart_category_categories` WHERE `category_parent_id`="'.$vm_catid.'"';
$db->setQuery($query);
$db->query();

$rows = $db->loadRowList();
if (empty($rows)) {
return;
} else {
foreach($rows as $row) {
array_push($ccont, $row[0]);
$kat = $row[0];
$this->untreeCat($kat, $ccont);
}
}
}



if ($virtuemart_category_id > 0){
   $joinCategory = TRUE ;
   $catscont = array();
   $this->untreeCat($virtuemart_category_id, $catscont);
   $qkat = ' `#__virtuemart_product_categories`.`virtuemart_category_id` IN('.$virtuemart_category_id;
    foreach ($catscont as &$kat){
       $qkat .= ', '.$kat;
    }
    $qkat .= ')';
    $where[] = $qkat;
}