Author Topic: Showing Categories in Product details default  (Read 269 times)

spike1968

  • Jr. Member
  • **
  • Posts: 140
Showing Categories in Product details default
« on: September 12, 2017, 11:21:21 am »
Versions: vm 3.2.4 Joomla 3.7.5

I am currently trying to create a list of categories relevant to the product. I want to display them at the bottom of the product page as tags.

Example: Product One is associated with 15 random categories I would like to display the categories associated with that product.

Related products is not suitable as that requires a manual action and I want it to be automated on every product.
Plugins are useful but I would prefer to code direct to the product_details template as that it is easier to control and manipulate.

I understand how to turn category id's into links etc, I understand mysql and understand intermediate php. What I am struggling with is the best way to import the data I need into the template.

Its the arrays at the beginning of templates that I struggle with, is there any documentation that could help me or could someone point me in the right direction.




spike1968

  • Jr. Member
  • **
  • Posts: 140
Re: Showing Categories in Product details default
« Reply #1 on: September 15, 2017, 11:28:49 am »
anyone....?

Spyros Petrakis

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 98
    • OlympianThemes
  • VirtueMart Version: Always latest Stable
Re: Showing Categories in Product details default
« Reply #2 on: September 16, 2017, 11:01:42 am »
Hi,
you can create the list with the following code:

Code: [Select]
<ul>
<?php
foreach($this->product->categoryItem as $tagcategory) {
    
$tagURL =  JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$tagcategory['virtuemart_category_id'], FALSE);
    
$tagName vmText::_($tagcategory['category_name']) ;
    echo 
'<li><a href="' $tagURL '">' $tagName '</a></li>';
}
?>

</ul>
Fast and simple to use templates with fanatical support
https://www.olympianthemes.com

spike1968

  • Jr. Member
  • **
  • Posts: 140
Re: Showing Categories in Product details default
« Reply #3 on: September 18, 2017, 08:29:54 am »
Thank you Spyros, I had actually come up with this solution late Friday night....

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

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="'. $category->category_name . '">'. $category->category_name .'</a></p>';

endforeach;

But your solution looks so much more efficient.

Many Thanks

spike1968

  • Jr. Member
  • **
  • Posts: 140
Re: Showing Categories in Product details default
« Reply #4 on: September 18, 2017, 08:38:15 am »
Just to confirm that Spryros solution is much better

Code: [Select]
<ul>
<?php
foreach($this->product->categoryItem as $tagcategory) {
$tagURL =  JRoute::_('index.php?option=com_virtuemart&view=category&virtuemart_category_id='.$tagcategory['virtuemart_category_id'], FALSE);
$tagName vmText::_($tagcategory['category_name']) ;
echo '<li><a href="' $tagURL '">' $tagName '</a></li>';
}
?>

</ul>