VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: encreplus on July 27, 2012, 22:49:29 PM

Title: [ SOLVED ]How to show all categories a product belong to ??
Post by: encreplus on July 27, 2012, 22:49:29 PM
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
Title: Re: How to show all categories a product belong to ??
Post by: Juraj on August 01, 2012, 22:53:59 PM
Hi, good question, it takes me too.
Title: Re: How to show all categories a product belong to ??
Post by: 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>';



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
Title: Re: How to show all categories a product belong to ??
Post by: Juraj on August 02, 2012, 17:56:05 PM
thanks ;-)
Title: Re: How to show all categories a product belong to ??
Post by: PRO 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
Title: Re: How to show all categories a product belong to ??
Post by: encreplus on August 02, 2012, 23:12:23 PM
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
Title: Re: How to show all categories a product belong to ??
Post by: ivus on August 02, 2012, 23:33:53 PM
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
Title: Re: How to show all categories a product belong to ??
Post by: encreplus 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
(
   
Title: Re: How to show all categories a product belong to ??
Post by: 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.
Title: Re: How to show all categories a product belong to ??
Post by: encreplus 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


Title: Re: How to show all categories a product belong to ??
Post by: plangdon 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
Title: Re: How to show all categories a product belong to ??
Post by: ivus 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.
Title: Re: How to show all categories a product belong to ??
Post by: plangdon 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.
Title: Re: How to show all categories a product belong to ??
Post by: 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
Title: Re: How to show all categories a product belong to ??
Post by: plangdon 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!
Title: Re: How to show all categories a product belong to ??
Post by: encreplus on August 23, 2012, 22:26:45 PM
Works great for me thank you !!!


Could someone help me with this ?  http://forum.virtuemart.net/index.php?topic=105917.msg354034#msg354034


Stefan
Title: Re: How to show all categories a product belong to ??
Post by: randomdev on November 13, 2012, 04:54:04 AM
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 ?
Title: Re: How to show all categories a product belong to ??
Post by: randomdev on November 13, 2012, 06:31:29 AM
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"; }

Title: Re: How to show all categories a product belong to ??
Post by: encreplus on July 20, 2013, 23:58:28 PM
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.
Title: Re: How to show all categories a product belong to ??
Post by: alizobil on January 04, 2014, 17:19:13 PM
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.
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: mikeboon on February 14, 2014, 15:27:45 PM
So many thanks. I looked for the whole afternoon for this!
It works!!!
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: shure348 on April 02, 2014, 08:56:06 AM
Hi all!
Many thanks for solution but a have a question!

How i can do this same in category (productlist) for each product??
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: capricorn on August 01, 2015, 00:00:47 AM
Thanks ivus! Exactly what I was looking for VM3.
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: PRO on August 02, 2015, 19:37:05 PM
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/>';
}
?>

Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: PRO on August 03, 2015, 13:57:44 PM
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>
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: capricorn on August 07, 2015, 19:09:02 PM
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.
Title: Re: How to show all categories a product belong to ??
Post by: 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 ;)
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: PRO on March 16, 2016, 12:41:34 PM
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?
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: encreplus on March 17, 2016, 13:35:45 PM
I need the categories to be sorted ascending ;)
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: PRO on March 20, 2016, 01:47:35 AM
Quote from: encreplus on March 17, 2016, 13:35:45 PM
I need the categories to be sorted ascending ;)


by name? or id?
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: encreplus on March 22, 2016, 14:32:34 PM
Quote from: PRO on March 20, 2016, 01:47:35 AM
Quote from: encreplus on March 17, 2016, 13:35:45 PM
I need the categories to be sorted ascending ;)


by name? or id?


By category name ;)
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: PRO on March 23, 2016, 14:57:23 PM
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");
?>
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: encreplus on March 28, 2016, 22:30:55 PM
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 , ,
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: Studio 42 on March 29, 2016, 00:46:36 AM
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/>';
}
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: encreplus on March 30, 2016, 04:50:38 AM
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
Title: Re: [ SOLVED ]How to show all categories a product belong to ??
Post by: Jörgen on March 30, 2016, 07:11:46 AM
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