VirtueMart Forum

VirtueMart 2 + 3 + 4 => Templating & Layouts => Topic started by: EvanGR on April 27, 2020, 11:14:09 AM

Title: [SOLVED] Product page: get any Category title by id?
Post by: EvanGR on April 27, 2020, 11:14:09 AM
In single product page, I have an array of category ids. I want to display the titles of these categories. Anyone have the code handy, or know where to find it?
Basically I want the function to get the category title by providing its id.

Thanks
Title: Re: Product page: get any Category title by id?
Post by: pinochico on April 27, 2020, 12:41:46 PM
Try this:

Quote
$active_category_id = vRequest::getInt('virtuemart_category_id', '0');
$category = $categoryModel->getCategory($active_category_id, false, false);
$catname = $category->category_name;
Title: Re: Product page: get any Category title by id?
Post by: EvanGR on April 28, 2020, 13:58:04 PM
Thanks, I am getting an error about '$categoryModel'. It's probably not defined in the product page. How do I reference that?

Thanks
Title: Re: Product page: get any Category title by id?
Post by: pinochico on April 28, 2020, 14:14:48 PM
Try this code before another:

$categoryModel = VmModel::getModel('Category');
Title: Re: Product page: get any Category title by id?
Post by: PRO on April 28, 2020, 17:57:32 PM
are you wanting the categories info the product is in? Like all the categories the product is in? The data is already available on product page.

Its available with     $this->product->categoryItem

The code below isnt exactly what you want, but you can see the variables and how i do it.

foreach ($this->product->categoryItem as $category){
if ($category['published']==0)continue;
if ($category['virtuemart_category_id']==85)continue;
$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'];

}
Title: Re: Product page: get any Category title by id?
Post by: EvanGR on April 28, 2020, 20:56:00 PM
Thank you very much!

I am not looking for the categories the product is in, in this case, but thanks for the info it will definitely be useful!