Author Topic: [ SOLVED ]How to show all categories a product belong to ??  (Read 15741 times)

encreplus

  • Jr. Member
  • **
  • Posts: 203
    • Dr-ink
  • VirtueMart Version: 3.0.10
Good day

Is there a way to show all the categories a product belongs to ...

The reason for this is im building a store selling inkjet and laser cartridges all my categories are printer models ... i would like that on each product details page to show all the categories this product belongs to ...

Is it possible ? How to achieve this ?

Stefan

Juraj

  • Beginner
  • *
  • Posts: 6
Re: How to show all categories a product belong to ??
« Reply #1 on: August 01, 2012, 22:53:59 pm »
Hi, good question, it takes me too.

ivus

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: How to show all categories a product belong to ??
« Reply #2 on: August 02, 2012, 02:13:11 am »
Hi peeps,

In your products template file (SITE_ROOT/components/com_virtuemart/views/productdetails/tmpl/default.php) <- should really be in your template overrides, but it's here so you know which file to look for.

Add this piece of code somewhere up the top.

Code: [Select]

$categoryModel = VmModel::getModel ('category');
$categoryArray = array();
foreach ($this->product->categories as $catid) :
$categoryArray[] = $categoryModel->getCategory ($catid, true);
endforeach;

echo '<pre>'; print_r($categoryArray); echo '</pre>';


Now you have all the category information stored in the $categoryArray variable. Unfortunately the function is a bit heavy, but it's the only way I can see it working without using core hacks.

Obviously I should tell you how to use the variable... I'm sure others would want to know too.

So where you want to output your category list, use this nifty piece of code:

Code: [Select]

foreach ($categoryArray as $category) :
echo '<p>'. $category->category_name .'</p>'; // <- STYLE HOWEVER YOU WANT
endforeach;


Done.

I hope this helps.  ;D

Juraj

  • Beginner
  • *
  • Posts: 6
Re: How to show all categories a product belong to ??
« Reply #3 on: August 02, 2012, 17:56:05 pm »
thanks ;-)

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10365
  • VirtueMart Version: 3+
Re: How to show all categories a product belong to ??
« Reply #4 on: August 02, 2012, 18:04:17 pm »
OR,

You can use "related categories"

Its not automated, BUT with CSV improved, you could do it easier
J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

encreplus

  • Jr. Member
  • **
  • Posts: 203
    • Dr-ink
  • VirtueMart Version: 3.0.10
Re: How to show all categories a product belong to ??
« Reply #5 on: August 02, 2012, 23:12:23 pm »
Hi peeps,

In your products template file (SITE_ROOT/components/com_virtuemart/views/productdetails/tmpl/default.php) <- should really be in your template overrides, but it's here so you know which file to look for.

Add this piece of code somewhere up the top.

Code: [Select]

$categoryModel = VmModel::getModel ('category');
$categoryArray = array();
foreach ($this->product->categories as $catid) :
$categoryArray[] = $categoryModel->getCategory ($catid, true);
endforeach;

echo '<pre>'; print_r($categoryArray); echo '</pre>';


How do i put this code ? Tried this but give me this code print on my page ...



Now you have all the category information stored in the $categoryArray variable. Unfortunately the function is a bit heavy, but it's the only way I can see it working without using core hacks.

Obviously I should tell you how to use the variable... I'm sure others would want to know too.

So where you want to output your category list, use this nifty piece of code:

Code: [Select]

foreach ($categoryArray as $category) :
echo '<p>'. $category->category_name .'</p>'; // <- STYLE HOWEVER YOU WANT
endforeach;


Done.

I hope this helps.  ;D

ivus

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: How to show all categories a product belong to ??
« Reply #6 on: August 02, 2012, 23:33:53 pm »
Hi encreplus,

almost didn't notice your question...

Quote
How do i put this code ? Tried this but give me this code print on my page ...

Did you read the rest of the post? the answer is in the code block directly underneath it.

Quote
Obviously I should tell you how to use the variable... I'm sure others would want to know too.

Just be sure to remove this line when you're done...

Code: [Select]
echo '<pre>'; print_r($categoryArray); echo '</pre>';
Simple if you just follow the instructions.  ;D

encreplus

  • Jr. Member
  • **
  • Posts: 203
    • Dr-ink
  • VirtueMart Version: 3.0.10
Re: How to show all categories a product belong to ??
« Reply #7 on: August 02, 2012, 23:44:18 pm »


It works in part ... i see the reuslts where it is supposed to be but at the beggining of the products detailed page is see this :

Array
(
   
  • => TableCategories Object

        (
            [virtuemart_category_id] => 2265
            [virtuemart_vendor_id] => 1
            [category_name] => Deskjet 3320
            [slug] => deskjet-3320
            [category_description] => Deskjet 3320
            ... ect ....




ivus

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: How to show all categories a product belong to ??
« Reply #8 on: August 03, 2012, 00:03:48 am »
Hi encreples,

OK.... like I said in my reply...?? seriously where in the instructions are you not understanding?

Code: [Select]

foreach ($categoryArray as $category) :
echo '<p>'. $category->category_name .'</p>'; // <- STYLE HOWEVER YOU WANT
endforeach;


Do you have this piece of code? It was I my original post.

Then to remove the debug output code...

Code: [Select]

echo '<pre>'; print_r($categoryArray); echo '</pre>';


Mentioned in my previous response.

Before I post any of my code, I test it on my blank install of J!/VM. I know this code works otherwise I wouldn't have responded.

encreplus

  • Jr. Member
  • **
  • Posts: 203
    • Dr-ink
  • VirtueMart Version: 3.0.10
Re: How to show all categories a product belong to ??
« Reply #9 on: August 03, 2012, 01:32:05 am »
Thanks ... it was me ... forgot to remove the debug code ;)

Works GREAT ...

Now just need to format it ;)

Can you help with this :  http://forum.virtuemart.net/index.php?topic=105917.0

Stefan



plangdon

  • Beginner
  • *
  • Posts: 3
Re: How to show all categories a product belong to ??
« Reply #10 on: August 21, 2012, 13:48:17 pm »
Hi Ivus,

Thanks a lot for this solution, it worked a treat.

I'd really like to be able to make this link clickable - so the name of each category links to its category page? Is there any way of doing this?

Thanks a lot for your help

ivus

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: How to show all categories a product belong to ??
« Reply #11 on: August 21, 2012, 13:51:56 pm »
Hi plangdon,

Of course. You could always wrap an anchor tag around it.

the URL can be found in the "/components/com_virtuemart/views/categories/tmpl/default.php" file.

Go nuts.

plangdon

  • Beginner
  • *
  • Posts: 3
Re: How to show all categories a product belong to ??
« Reply #12 on: August 21, 2012, 17:25:03 pm »
Thanks for that? Been trying to figure out exactly how to put the php together to make these link but can't get it right? If you could give more detail that'd be great.

Really appreciate your help.

ivus

  • Full Member
  • ***
  • Posts: 534
  • - YAY me... proud member of the 500 club...
Re: How to show all categories a product belong to ??
« Reply #13 on: August 21, 2012, 17:36:09 pm »
Hi plangdon,

You should participate in my poll https://forum.virtuemart.net/index.php?topic=106686.0

Anyway. In that file I recommended you open, there's a comment line that clearly states the following:

Code: [Select]

    // Category Link
    $caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category->virtuemart_category_id );


so if you combine that with the code I gave you previously, following html standard, you should produce the following:

Code: [Select]

foreach ($categoryArray as $category) :

// Category Link
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category->virtuemart_category_id );

echo '<p><a href="'.$caturl .'" class="whatEverYouLike">'. $category->category_name .'</a></p>'; // <- STYLE HOWEVER YOU WANT

endforeach;


I hope that's enough information for you. ;D

plangdon

  • Beginner
  • *
  • Posts: 3
Re: How to show all categories a product belong to ??
« Reply #14 on: August 22, 2012, 11:39:26 am »
Thanks so much - this works perfectly, you've been a great help

I'll do your poll now!