I'm a bit of a newbie at PHP and OpenCart so thought I'd ask if anyone else had done this already before I attempt it myself.
I'm not using breadcrumbs and I have many products in more than one category. To aid navigation what I'd like to do is to show a list of all the categories a product belongs in.
I'm guessing an edit of the category module would be the best way to go about this, starting with getCategories($product_id), but that's about as far as I've got so far.
Any help would be most appreciated.
http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk
After connecting to the database I ran this:
Code: Select all
$get_cats_id = "SELECT category_id
FROM product_to_category
WHERE product_id = '".$product_id."'";
$get_cats_res = mysql_query($get_cats_id);
while ($cats = mysql_fetch_array($get_cats_res, MYSQL_ASSOC))
{
$cat_id = $cats['category_id'];
$get_cat_name = "SELECT name
FROM category_description
WHERE category_id = '".$cat_id."'";
$get_name_res = mysql_query($get_cat_name);
$name = mysql_fetch_array($get_name_res, MYSQL_ASSOC);
$get_cat_url = "SELECT keyword
FROM url_alias
WHERE query = 'category_id=".$cat_id."'";
$get_url_res = mysql_query($get_cat_url);
$url = mysql_fetch_array($get_url_res, MYSQL_ASSOC);
$get_par_id = "SELECT parent_id
FROM category
WHERE category_id = '".$cat_id."'";
$get_par_res = mysql_query($get_par_id);
$par = mysql_fetch_array($get_par_res, MYSQL_ASSOC);
$get_par_url = "SELECT keyword
FROM url_alias
WHERE query = 'category_id=".$par['parent_id']."'";
$get_purl_res = mysql_query($get_par_url);
$purl = mysql_fetch_array($get_purl_res, MYSQL_ASSOC);
echo "<a href=\"http://www.example.com/".$purl['keyword']."/".$url['keyword']."/\">".$name['name']."</a> ";
mysql_free_result($get_name_res);
mysql_free_result($get_url_res);
mysql_free_result($get_par_res);
mysql_free_result($get_purl_res);
}
mysql_free_result($get_cats_res);
http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk
Think it's great, but first time I've installed it. Is there a list of functions and how to use them, as there is with the Wordpress Codex? If so, I've missed it. If not, might be a useful thing to implement in the Wiki going forward.
http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk
Put the following code in to catalog/model/category.php:
Code: Select all
public function getProductCategories($prodid) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . $prodid . "'";)
if($query->num_rows > 0) return $query->rows;
return false;
}
Code: Select all
$prodcategories = $this->model_catalog_category->getProductCategories($myproductidhere);
$cats = array();
if($prodcategories) {
foreach($prodcategories as $prodcategory) {
$cats[] = $prodcategory['category_id'];
}
$this->data['cats'] = $cats;
}
$this->data['cats'] will contain an array of all category id's assocatied with that product
I'm struggling to get my head round this, does anyone know how to implement this so that it will show the categories a product belongs to on the product page.
So far I have added the following code:
catalog/model/catalog/category.php
Code: Select all
public function getProductCategories($prodid) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . $prodid . "'";)
if($query->num_rows > 0) return $query->rows;
return false;
}
Code: Select all
$prodcategories = $this->model_catalog_category->getProductCategories($product_id);
$cats = array();
if($prodcategories) {
foreach($prodcategories as $prodcategory) {
$cats[] = $prodcategory['category_id'];
}
$this->data['cats'] = $cats;
Cheers
Gaz
Code: Select all
<?php
$this->load->model('catalog/category');
$this->load->model('tool/seo_url');
$query = $this->db->query("SELECT category_id
FROM product_to_category
WHERE product_id = '".$product_id."'");
$prodcategories = $query->rows;
foreach($prodcategories as $prodcategory){
$category_id = $prodcategory['category_id'];
$category_info = $this->model_catalog_category->getCategory($category_id);
$caturl = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $category_id); ?>
<a href="<?php echo $caturl; ?>"><?php echo $category_info['name']; ?></a>
<br />
<?php } ?>
http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk
Find and get many various of opencart modules, themes, mods, etc for your opencart store at http://www.openmycart.com/oc/, OPENCART SITE customization and Maintenance supports at here
http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk
http://scarletandjones.com
http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk
Code: Select all
$query = $this->db->query("SELECT parent_id
FROM category
WHERE category_id = '".$category_id."'");
$parentcategories = $query->rows;
http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk
Undefined index: name in ..catalog/view/theme/xxxx/template/product/product.tpl
in this line:
<td><a href="<?php echo $caturl; ?>"><?php echo html_entity_decode($category_info['name'], ENT_QUOTES, 'UTF-8'); ?></a><br />
It's working only for 1 category product
OpenCart :
The best Open Source Cart for eCommerce
Users browsing this forum: No registered users and 98 guests