Post by solojuve1897 » Fri Jan 13, 2012 11:09 pm

Hello,

I was wondering if somebody could help me with listing all the categories which a certain product belongs to.

For example. The product "Apple Cinema 30"" in my page has been linked to 5 sub-categories under the main-category "Components". How can I on the product page list all the sub-categories which is linked to the product?

The desired output:
This product belongs to the following sub-categories: Mice and Trackballs, Monitors, Printers, Scanners, Web Cameras

I'm using 1.5.1.3

Any help is appreciated!

Regards,
Last edited by solojuve1897 on Sat Jan 14, 2012 2:59 am, edited 1 time in total.

Newbie

Posts

Joined
Fri Jun 03, 2011 1:00 am

Post by angelo » Sat Jan 14, 2012 1:32 am

Do you really need it? :)

Anyway, the association of a product to categories is stored in the db table "product_to_category". You may want to start from there...

A.

New member

Posts

Joined
Tue Dec 06, 2011 8:23 pm

Post by solojuve1897 » Sat Jan 14, 2012 2:34 am

Hello again,

I really didnt think I was going to pull this through. My first time I manipulate OpenCart in this fashion (way to advanced for me). Here is what I did:

Added in model/catalog/product.php:

Code: Select all

public function getProductCategories($product_id) {

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category pc, " . DB_PREFIX . "category_description cd WHERE pc.product_id = '" . (int)$product_id. "' AND cd.category_id = pc.category_id");



		return $query->rows;

	}
Added in controller/product/product.php:

Code: Select all

$this->data['product_categories'] = array();	
			$results = $this->model_catalog_product->getProductCategories($this->request->get['product_id']);

			foreach ($results as $result) {

				$this->data['product_categories'][] = array(

					'tag'  => $result['name'],

					'href' => $this->url->link('product/category', $url . '&path=' . $result['category_id'])
				);

			}
Added in product.tpl:

Code: Select all

<?php if ($product_categories) { ?>

  <div class="test"><br /><b>Denna produkt passar &auml;ven:</b>

    <?php foreach ($product_categories as $product_category) { ?>
		<?php if($product_category['tag'] != ''){?>
    		<a href="<?php echo $product_category['href']; ?>"><?php echo $product_category['tag']; ?></a>,
		<?php }?>
    <?php } ?>
	<br /><br />
  </div>

  <?php } ?>
Result:

http://tonera.se/index.php?route=produc ... duct_id=42

"This product belongs to the following sub-categories: Monitors, Mice and Trackballs, Printers, Scanners, Web Cameras,"

This modifcation is useful when creating a Cartridge-site. "This toner also fits to:" and then the list of printer-models.
A printer model can have different cartridges connected to it, and also the cartridges can fit to many different printer models, so the best way, for the customer, was to put all the printer-models as categories. And then just connect the different products to the categories.

Newbie

Posts

Joined
Fri Jun 03, 2011 1:00 am
Who is online

Users browsing this forum: No registered users and 41 guests