News:

Looking for documentation? Take a look on our wiki

Main Menu

[ SOLVED ]How to show all categories a product belong to ??

Started by encreplus, July 27, 2012, 22:49:29 PM

Previous topic - Next topic

encreplus

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


ivus

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.



$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:



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



Done.

I hope this helps.  ;D


PRO

OR,

You can use "related categories"

Its not automated, BUT with CSV improved, you could do it easier

encreplus

Quote from: ivus 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.



$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:



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



Done.

I hope this helps.  ;D

ivus

Hi encreplus,

almost didn't notice your question...

QuoteHow 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.

QuoteObviously 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...

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

Simple if you just follow the instructions.  ;D

encreplus



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

Hi encreples,

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



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...



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

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

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

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

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

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:



    // 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:



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

Thanks so much - this works perfectly, you've been a great help

I'll do your poll now!