Post by mdziwuls » Thu Jul 02, 2015 8:34 am

Hey Everyone,

I'm a bit of a newbie at OpenCart I have a question.

I have many products in more than one category. To help with site navigation I'd like to show a list of all the categories a product belongs in, in the products page.

Example:

Product x belongs to categories Z,Y,W. When one navigates to the page of product X on the bottom I'd like it to show "this product is compatible with:" then show categories Z,Y, and W.

Ive tried multiple ways and am getting error codes. I have been messing with the category.php file and the product.php file. Any ideas?


Thanks in advance

Newbie

Posts

Joined
Thu Jul 02, 2015 8:22 am

Post by mdziwuls » Fri Jul 03, 2015 12:33 am

So far I have

catalog/model/catalog/category.php
----------------------------------------------------
public function getProductCategories($prodid) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "oc_product_to_category WHERE product_id = '" . $prodid . "'";)
if($query->num_rows > 0) return $query->rows;
return false;
}

catalog/controller/product/product.php
------------------------------------------------
$prodcategories = $this->model_catalog_category->getProductCategories($product_id);
$cats = array();
if($prodcategories) {
foreach($prodcategories as $prodcategory) {
$cats[] = $prodcategory['category_id'];
}
$this->data['cats'] = $cats;

I don't know what else to do, how would I display it in the product template file.

Newbie

Posts

Joined
Thu Jul 02, 2015 8:22 am

Post by Rohit0071996 » Wed Jul 15, 2015 11:02 pm

mdziwuls wrote:So far I have

catalog/model/catalog/category.php
----------------------------------------------------
public function getProductCategories($prodid) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "oc_product_to_category WHERE product_id = '" . $prodid . "'";)
if($query->num_rows > 0) return $query->rows;
return false;
}

catalog/controller/product/product.php
------------------------------------------------
$prodcategories = $this->model_catalog_category->getProductCategories($product_id);
$cats = array();
if($prodcategories) {
foreach($prodcategories as $prodcategory) {
$cats[] = $prodcategory['category_id'];
}
$this->data['cats'] = $cats;

I don't know what else to do, how would I display it in the product template file.
you can foreach the data['cats'] array in the file - catalog/view/product/product.tpl by using the array variable name - $cats
for example, to display all the categories which data['cats'] has stored, do this in the product.tpl file -

Code: Select all

foreach($cats as $cat)
{
echo $cat.", ";
}
Hope it helped.

New member

Posts

Joined
Fri Apr 03, 2015 6:44 pm
Who is online

Users browsing this forum: No registered users and 2 guests