0

I want to show the category name of a product, inside the category product listing div (category name inside each product div). I use this code to show manufacturer:

<?php
$db = JFactory::getDBO();
$db->setQuery('SELECT category_name FROM '.hikashop_table('category').'
WHERE category_id='.(int)$this->row->product_manufacturer_id);
$brandName = $db->loadResult();
?>
<?php
if (!empty($brandName)) {
echo JText::_('MANUFACTURER').': '. $brandName;
} else { echo " ";}
?>

Any help? Thank you

  • You shouldn't put database queries inside loops. This is highly inefficient. You'll run a query for each product and end up with duplicate queries (in case multiple products have the same manufacturer). It's better to collect manufacturer IDs, run a single query to get their names and make them accessible in an easy way (e.g. indexed array). – Sharky Apr 29 at 8:55
1

This code will display the brand name of each product on the listing. For the category name, you would need to do something like that instead:

<?php
$db = JFactory::getDBO();
$db->setQuery('SELECT category_name
               FROM ' . hikashop_table('category') . ' AS c
               LEFT JOIN ' . hikashop_table('product_category') . ' AS pc
                   ON c.category_id = pc.category_id
               WHERE pc.product_id = ' . (int)$this->row->product_id);
$categoryName = $db->loadResult();
echo $categoryName;
?>

To get a link around the category name, you would do instead:

<?php
$db = JFactory::getDBO();
$db->setQuery('SELECT category_name
               FROM ' . hikashop_table('category') . ' AS c
               LEFT JOIN ' . hikashop_table('product_category') . ' AS pc
                   ON c.category_id = pc.category_id
               WHERE pc.product_id = ' . (int)$this->row->product_id);
$category = $db->loadObject();
$class = hikashop_get('class.category');
$class->addAlias($category);
echo '<a href="'.hikashop_contentLink('category&task=listing&cid='.$category->category_id.'&name='.$category->alias,$category).'">'.($category->category_name).'</a>';
?>
  • Is there a way to have the category name as link to that specific category products listing? Thanks. – geoplous Apr 30 at 15:15
  • I've modified the code in my message above to add the code for the category with the link. – Nicolas May 2 at 8:29
  • Thank you, works fine! – geoplous May 2 at 13:45
0

As far as I can tell, you don't need to query the database. Category information is available through categories property.

// Get category names.
$categories = array_column($this->row->categories, 'category_name');
echo implode(', ', $categories);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.