News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

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


randomdev

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 ?

randomdev

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"; }


encreplus

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

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.

alizobil

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.

mikeboon

So many thanks. I looked for the whole afternoon for this!
It works!!!

shure348

Hi all!
Many thanks for solution but a have a question!

How i can do this same in category (productlist) for each product??

capricorn


PRO

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/>';
}
?>


PRO

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>

capricorn

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.

encreplus

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 ;)

PRO

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?

encreplus


PRO