Author Topic: Displaying all products under sub-categories too  (Read 68448 times)

lashae

  • Beginner
  • *
  • Posts: 9
Displaying all products under sub-categories too
« on: August 11, 2006, 22:44:04 pm »
Hi all,

if you need to display all products under the sub-categories of the current category too, you can simply do the following.

1. Open administrator/components/com_virtuemart/html/shop_browse_queries.php

2. At the top of the file after the line starting with mm_showMyFileName... insert the following;

Code: [Select]
function untreeCat($kat, &$container)
{
$parentsql = mysql_query("SELECT category_child_id FROM portal_vm_category_xref WHERE category_parent_id=$kat");
// if it is a leaf (no data underneath it) then return
if (!mysql_num_rows($parentsql))
{
return;
}
//else append the result, and recurse the function (so to speak)
else
{
while($dataset = mysql_fetch_array($parentsql))
{
  $container[] = $dataset['category_child_id'];
  $kat = $dataset['category_child_id'];
  untreeCat($kat, $container);
}
}
}

3. Then, search for the following snippet;

Code: [Select]
if( $category_id ) {
  $q .= "\n AND `#__{vm}_product_category_xref`.`category_id`='".$category_id."' ";
}

replace it with the following;

Code: [Select]
if( $category_id )
{
$kategoriListesi = array();
$q .= "\n AND `#__{vm}_product_category_xref`.`category_id`='".$category_id."' ";
untreeCat($category_id, $kategoriListesi);

foreach ($kategoriListesi as $kat)
{
$q .= "\n OR `#__{vm}_product_category_xref`.`category_id`='".$kat."' ";
}
}

Then enjoy ;-)

Edit: Of course to add that; the portal_vm phrase should correspond to your Joomla and VM installation DB prefixes.

mknz

  • Beginner
  • *
  • Posts: 31
    • National Underground
Re: Displaying all products under sub-categories too
« Reply #1 on: September 07, 2006, 03:07:57 am »
absolutely amazing!  Very easy to put together, but I need to be able to display what sub-cat each item is in.
-OR-
If it could just display the first category below and then the child items of the items in there, it would work...let me explain.

My setup is for a music site that is laid out like this:
Band->Releases->Album->Tracks
where the album can be bought physically as CD or LP and tracks can be purchased as downloads individually or as the complete album.

also band merchandise is laid out:
Band->Merch->T-Shirts->Item

I've tried parent/child setups for the album/tracks; I've tried this hack to show everything in the band category, but I can't make it display the items displayed in their appropriate category from one page like this:

Band Category

   Picture & Description

Releases - No Description ('cause I didn't add one)

   Album One - Picture & Description

      Track 1 Mp3 | Sample | Add to Cart
      Track 2 Mp3
      Track 3 Mp3
      CD
      LP
      MP3

   Album Two - Picture & Description

      Track 1 Mp3 | Sample | Add to Cart
      Track 2 Mp3 | Sample | Add to Cart
      Track 3 Mp3 | Sample | Add to Cart
      CD | Add to Cart
      LP | Add to Cart
      MP3 | Add to Cart

Very interested to find a solution...I'm just not any good at PHP...THANX A MILLION!

-mknz

MartijnBoeve

  • Beginner
  • *
  • Posts: 5
Re: Displaying all products under sub-categories too
« Reply #2 on: November 23, 2006, 15:05:43 pm »
I think this is just what I'm looking for but I'm encountering some problems...

I've integrated the code and it does show products in the main category, but it displays all products and not just the ones from the sub-categories, also it should display 4 products in a row, but it displays 3 in a row (i have the right shop_browse page and the # of products in a row is set to 4)...

is it supposed to show just all products or the products it finds in the subcats? Also loading the page takes much longer.

Has anyone encountered these problems ans solved them? I would be eternaly thankfull!
Martijn


Creb

  • Beginner
  • *
  • Posts: 18
Re: Displaying all products under sub-categories too
« Reply #3 on: December 20, 2006, 16:29:14 pm »
I modified to last Code

Code: [Select]

if( $category_id ) {
  $q .= "\n AND `#__{vm}_product_category_xref`.`category_id`='".$category_id."' ";
}


replace it with the following;

Code: [Select]
if( $category_id )
{
$kategoriListesi = array();
untreeCat($category_id, $kategoriListesi);
$qkat = "\n AND `#__{vm}_product_category_xref`.`category_id` IN(".$category_id . ",";
foreach ($kategoriListesi as $kat)
{
$qkat .= $kat . ',';
}
$qkat .= ")";

$qkat = str_replace(',)', ')', $qkat);
$q .= $qkat;
}

This working OK

Creb

Creb

  • Beginner
  • *
  • Posts: 18
Re: Displaying all products under sub-categories too
« Reply #4 on: March 26, 2007, 02:38:18 am »
In VM 1.0.10

repleace it for this
Code: [Select]
if( $category_id ) {
//$where_clause[] = "`#__{vm}_product_category_xref`.`category_id`=".$category_id;
$kategoriListesi = array();
untreeCat($category_id, $kategoriListesi);
$qkat = " `#__{vm}_product_category_xref`.`category_id` IN(".$category_id . ",";
foreach ($kategoriListesi as $kat)
{
$qkat .= $kat . ',';
}
$qkat .= ")";

$qkat = str_replace(',)', ')', $qkat);
$where_clause[] = $qkat;

}

Creb

FritoBandito

  • Beginner
  • *
  • Posts: 4
Re: Displaying all products under sub-categories too
« Reply #5 on: June 20, 2007, 08:51:10 am »
Thanks Creb!  Worked perfectly!

Martin

  • Beginner
  • *
  • Posts: 1
Re: Displaying all products under sub-categories too
« Reply #6 on: June 23, 2007, 13:28:18 pm »
It works but it displays all products under the sub-categories too.
I just want the mod to display all products under the parent category.
Basically what i want is a category like this:

Cameras->All Products
           ->Outdoor
           ->Indoor

If that's not possible i want Cameras to display all products.
Is there a way to get this done easily?

Thanks. :)

Francois1

  • Beginner
  • *
  • Posts: 5
    • francoissmit.com
Re: Displaying all products under sub-categories t
« Reply #7 on: July 12, 2007, 22:22:31 pm »
Thanks Creb and lashae

The 2 snippets of code does not seem to work in version 1.0.12 any more. Is there anything I can do to fix this.
Thanks in advance.

haselnuss

  • Beginner
  • *
  • Posts: 1
Re: Displaying all products under sub-categories too
« Reply #8 on: August 01, 2007, 16:25:36 pm »
Hi.
Thanks to lashae and Creb for the code snippets.
I've rewritten them a little just to store the "untreeCat" in ps_product_category.php and to use the ps_DB class. I tested on VM 1.0.12 and it works.

1. In administrator/components/com_virtuemart/html/shop_browse_queries.php

replace
Code: [Select]
if( $category_id ) {
$where_clause[] = "`#__{vm}_product_category_xref`.`category_id`=".$category_id;
}
with
Code: [Select]
if( $category_id ) {
//$where_clause[] = "`#__{vm}_product_category_xref`.`category_id`=".$category_id;
$kategoriListesi = array();
$ps_product_category->untreeCat($category_id, $kategoriListesi);
$qkat = " `#__{vm}_product_category_xref`.`category_id` IN(".$category_id . ",";
foreach ($kategoriListesi as $kat)
{
$qkat .= $kat . ',';
}
$qkat .= ")";

$qkat = str_replace(',)', ')', $qkat);
$where_clause[] = $qkat;

}

2. In administrator/components/com_virtuemart/classes/ps_product_category.php
add the "untreeCat" function at the end of the class
Code: [Select]
/*
* Returns an array of the categories ids recursively for a given category
* untreeCat © lashae (virtuemart forum http://virtuemart.net/index.php?option=com_smf&Itemid=71&topic=20837.0)
* @param int $kat
* @param int $container
* rewritten by haselnuss (to use ps_DB class and store the function in ps_product_category.php)
*/
function untreeCat($kat, &$container) {
$db = new ps_DB;
$q = "SELECT `category_child_id` FROM `#__{vm}_category_xref` WHERE `category_parent_id`=$kat";
$db->query($q);
// if it is a leaf (no data underneath it) then return
if (!$db->num_rows()) {
return;
}
//else append the result, and recurse the function (so to speak)
else
{
while($db->next_record()) {
  $container[] = $db->f("category_child_id");
  $kat = $db->f("category_child_id");
  $this->untreeCat($kat, $container);
}
}
}

Hope this helps someone.

Cya


Timothy

  • Beginner
  • *
  • Posts: 6
Re: Displaying all products under sub-categories t
« Reply #9 on: August 31, 2007, 18:07:34 pm »
Thank you!  It works perfectly.  I just wish I would have found this sooner than after searching for this hack for over an hour.  It seems strange that this is not the default behavior or an checkable option in the admin.

Techno-Tronics

  • Beginner
  • *
  • Posts: 39
Re: Displaying all products under sub-categories t
« Reply #10 on: August 31, 2007, 18:52:36 pm »
Can't find shop_browse_queries.php anywhere, think this isn't in the normal installation ?

Where to download the file ?

emfglobal

  • Beginner
  • *
  • Posts: 11
Re: Displaying all products under sub-categories too
« Reply #11 on: October 30, 2007, 16:05:11 pm »
I am currently using VM 1.0.12 integrated into Joomla of course.. (What else)

But I can not get this to work for me.
I can get all products to show up under a sub cat, but what I would like is to only show produckts from that particular categorie,
for example,

I sell cat stuff and dog stuff

under Dog stuff I have Food, Toys, and Other

Under Food I have Cokkies  and Other

Here I would like to show all Dog Food..


But I would alos like to show all dog stuff under the Categories when you click on Dogstuff..
The reason is i have many productks and catagoeries and sub categories, but i would like to make it as easy as possible for anyone to shop for the correct item...


I hope you get what I am looking for..
If you know what you are doing, you could also PM me and I will show you in the site what I am talking about...

thanks for any help...
Espen


meehal

  • Beginner
  • *
  • Posts: 15
Re: Displaying all products under sub-categories too
« Reply #12 on: March 05, 2008, 13:40:01 pm »

I've loaded this up and its a great tool, has anyone figured out have to get the category name in the output to break it up?

Or any ideas which part i might look at to modify and get me in the right direction?

thank you.

FavoriteU

  • Jr. Member
  • **
  • Posts: 119
Re: Displaying all products under sub-categories too
« Reply #13 on: April 01, 2008, 01:22:10 am »
I want to thank lashae, Creb and Haselnuss for this.  I have been looking for a way to do this for a while and somehow missed this post.  It works great!

I'm sure there are others out there looking for the same thing.

scha

  • Beginner
  • *
  • Posts: 2
Re: Displaying all products under sub-categories too
« Reply #14 on: May 21, 2008, 12:45:51 pm »
Hi there, thank you :)

In VM 1.0.15 i had a "Call to a member function on a non-object in shop_browse_queries.php" on that line:
Code: [Select]
$ps_product_category->untreeCat($category_id, $kategoriListesi); when trying to view a product detail (categories, subcats and overview were working perfectly fine!)
i added
Code: [Select]
global $ps_product_category; somewhere before the erronous part and its now working, but my code understandings for oop are somewhat small.. Can anyone tell me if there is a better (or righter) solution for that problem?

regards