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
Hi, good question, it takes me too.
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
thanks ;-)
OR,
You can use "related categories"
Its not automated, BUT with CSV improved, you could do it easier
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
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
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 ....
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.
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
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
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.
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.
Hi plangdon,
You should participate in my poll https://forum.virtuemart.net/index.php?topic=106686.0 (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
Thanks so much - this works perfectly, you've been a great help
I'll do your poll now!
Works great for me thank you !!!
Could someone help me with this ? http://forum.virtuemart.net/index.php?topic=105917.msg354034#msg354034
Stefan
I have a problem that is somewhat related to this. I want to display a custom message on the email to the customer if they have a product that belongs to a particular category on there.
The problem is when I use
foreach($this->orderDetails['items'] as $item) { echo $item->virtuemart_category_id; }
It will only show a single product category for each product but I need to check all of the categories a product belongs to.
Does anyone have an idea how I can check if a product belonging to a particular category is on the order, from the file: views\invoice\tmpl\mail_html_shoppper.php ?
I managed to get it working with the following code but it does seem a bit slow to checkout (not sure if it is related to my code)
Could anyone tell me if there is a better way to do this?
$product_model = VmModel::getModel('product');
$pwarning = 0;
foreach($this->orderDetails['items'] as $item) {
$product = $product_model->getProduct($item->virtuemart_product_id);
foreach ($product->categories as $catg) {
if ($catg==167) { $pwarning = 1;}
}
}
if ($pwarning == 1) { echo "Warning: etc"; }
Hello this code was working until i upgraded to 2.0.22a ... now it dosent work anymore ... any clue why ?
The error that i have is this :
Warning: Invalid argument supplied for foreach() in /home/drink/public_html/templates/vp_promart/html/com_virtuemart/productdetails/default.php on line 166
Quote from: ivus 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 (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
Quote from: ivus 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?
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.
Quote from: ivus 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?
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.
Hello, I know this topic is quite old, but it works great, I have all my category URL links display in my product page. Thanks to Ivus!
I'm wondering if it is also possible to display a thumbnail image of the category next to the URL link?
Thanks for anyone who could help on this as well.
So many thanks. I looked for the whole afternoon for this!
It works!!!
Hi all!
Many thanks for solution but a have a question!
How i can do this same in category (productlist) for each product??
Thanks ivus! Exactly what I was looking for VM3.
Quote from: capricorn on August 01, 2015, 00:00:47 AM
Thanks ivus! Exactly what I was looking for VM3.
for vm3, the categories are already there on the product page.
They are in an array $this->product->categoryItem
no need to do a lot of the code posted in this thread
This will make the link to the categories
<?php $categories=$this->product->categoryItem;
foreach ($categories as $category){
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category['virtuemart_category_id'] );
$catname=$category['category_name'];
echo '<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a><br/>';
}
?>
Guys, I wrote this code for my site.
Its for product details
It combines
Child Categories
All categories a product is in
& Related Categories
<div class="category-view related-childs related-categories">
<?php
$i=1;
$html='';
foreach ($this->product->categoryItem as $category){
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category['virtuemart_category_id'] );
$catname=$category['category_name'];
$categoriescombined[]= array($caturl,$catname,$category['virtuemart_category_id']);
$skip2[]=$category['virtuemart_category_id'];
}
foreach ($this->category->children as $category) {
if (in_array($category->virtuemart_category_id,$skip2, TRUE)){
continue;
}
$caturl=JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category->virtuemart_category_id, FALSE);
$catname=$category->category_name;
$categoriescombined[]= array($caturl,$catname,$category->virtuemart_category_id);
}
// print_r($categoriescombined[2]);
$html.='<h3>Related Categories</h3>';
$html.='<ul class="ul width30">';
foreach ($this->product->customfieldsSorted[related_categories] as $field) {
$skip[]=$field->customfield_value;
$html.='<li class="i'.$i.'">'.$field->display.'</li>';
$i++;
if ($i==5){$html.='</ul><ul class="ul">';}
}
foreach ($categoriescombined as $categoriescombined){
if (in_array($categoriescombined[2],$skip, TRUE)){
continue;
}
$html.='<li class="i'.$i.'"><a href="'.$categoriescombined[0].'" title="'.$categoriescombined[1].'">'.$categoriescombined[1].'</a></li>';
$i++;
if ($i==5){$html.='</ul><ul class="ul">';}
}
$html.='</ul>';
echo $html;
?>
</div>
Hi Pro,
Thanks for the code. The problem I'm facing with your code, as with the code from ivus might be relevant only to me. Therefore I will start a new topic. Please advise.
http://forum.virtuemart.net/index.php?topic=131020.msg451568#msg451568
Thanks.
Good day,
Im using this code :
<?php $categories=$this->product->categoryItem;
foreach ($categories as $category){
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category['virtuemart_category_id'] );
$catname=$category['category_name'];
echo '<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a><br/>';
}
?>
What i need is to be all after each other .. and sorted ... ie : cat1, cat2, cat3, cat4, cat5, ect...
How can i do this ;)
Quote from: encreplus on March 15, 2016, 21:17:59 PM
What i need is to be all after each other .. and sorted ... ie : cat1, cat2, cat3, cat4, cat5, ect...
what do you mean?
I need the categories to be sorted ascending ;)
Quote from: encreplus on March 17, 2016, 13:35:45 PM
I need the categories to be sorted ascending ;)
by name? or id?
Quote from: encreplus on March 15, 2016, 21:17:59 PM
Good day,
Im using this code :
<?php $categories=$this->product->categoryItem;
foreach ($categories as $category){
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category['virtuemart_category_id'] );
$catname=$category['category_name'];
echo '<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a><br/>';
}
?>
What i need is to be all after each other .. and sorted ... ie : cat1, cat2, cat3, cat4, cat5, ect...
How can i do this ;)
see what happens when you place this code above that code
<?php
function sortByOption($a, $b) {
return strcmp($a['category_name'], $b['category_name']);
}
usort($this->product->categoryItem['category_name'], "sortByOption");
?>
With the sort code above ... nothing change ...
the category stays like this ...
HP Deskjet 1000 , HP DeskJet 1010 , HP Deskjet 1051 , HP DeskJet 1510 , HP DeskJet 1056 , HP DeskJet 1055 , HP DeskJet 1050 , HP DeskJet 1513 , HP Deskjet 1512 , HP DeskJet 2050 , HP Deskjet 2510 , HP Deskjet 2512 , HP Deskjet 2514 , HP DeskJet 2540 , HP DeskJet 2541 , HP DeskJet 2542 , HP DeskJet 2543 , HP DeskJet 2544 , HP DeskJet 2547 , HP Deskjet 3000 , HP DeskJet 3050 , HP DeskJet 3050A , HP DeskJet 3051A e-All-in-One J611h , HP DeskJet 3052A Wireless e-All-In-One , HP DeskJet 3054 , HP DeskJet 3056A , HP DeskJet 3510 , HP DeskJet 3511 , HP DeskJet 3512 , HP DeskJet 3516 , HP ENVY 4500 e-All-in-One , HP ENVY 4501 e-All-in-One , HP ENVY 4502 e-All-in-One , HP ENVY 4503 e-All-in-One , HP ENVY 4504 e-All-in-One , HP ENVY 4505 e-All-in-One , HP ENVY 5530 e-All-in-One , HP ENVY 5531 e-All-in-One , HP ENVY 5538 e-All-in-One , HP OfficeJet 2620 , HP OfficeJet 4630 , HP OfficeJet 4632 , HP OfficeJet 4634 , HP OfficeJet 4635 , ,
Try usort($this->product->categoryItem, "sortByOption");
else :
<?php
$categories= array();
foreach ($this->product->categoryItem as $category) $categories[$category['category_name']]=$category ;
ksort($categories);
foreach ($categories as $category){
$caturl = JRoute::_ ( 'index.php?option=com_virtuemart&view=category&virtuemart_category_id=' . $category['virtuemart_category_id'] );
$catname=$category['category_name'];
echo '<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a><br/>';
}
it work in some part ... but it put for this example all the HP DeskJet before the HP Deskjet ... ie ... it put the CAPS of J before the j ...
is there a way to fix this ?
Thanks a lot for this help ... really appreciated
Hello
You could change the flags for the sort:
ksort($categories, SORT_NATURAL | SORT_FLAG_CASE);
More info here:
http://php.net/manual/en/function.sort.php (http://php.net/manual/en/function.sort.php)
regards
Jörgen @ Kreativ Fotografi