VirtueMart Forum

VirtueMart 2 + 3 + 4 => Templating & Layouts => Topic started by: Agaton on September 12, 2017, 11:21:21 AM

Title: Showing Categories in Product details default
Post by: Agaton 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.



Title: Re: Showing Categories in Product details default
Post by: Agaton on September 15, 2017, 11:28:49 AM
anyone....?
Title: Re: Showing Categories in Product details default
Post by: Spiros Petrakis on September 16, 2017, 11:01:42 AM
Hi,
you can create the list with the following code:


<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>
Title: Re: Showing Categories in Product details default
Post by: Agaton on September 18, 2017, 08:29:54 AM
Thank you Spyros, I had actually come up with this solution late Friday night....

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


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
Title: Re: Showing Categories in Product details default
Post by: Agaton on September 18, 2017, 08:38:15 AM
Just to confirm that Spryros solution is much better

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