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
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.
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 - $catsmdziwuls 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.
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.", ";
}
Who is online
Users browsing this forum: No registered users and 2 guests