Post by cemas » Fri Apr 15, 2011 3:30 am

Hi, maybe someone can help me with product category id? I need to show in product page a category ID. I use this script:
if(isset($_GET['path'])) {
$path = $_GET['path'];
$cats = explode('_', $path);
$cat_id = $cats[count($cats) - 1];

}

but it works only then in product page going via categories page, then going to product page via latest or special it shows error:
Notice: Undefined variable: cat_id in /home/madairstil/domains/madairstilius.lt/public_html/oc/catalog/view/theme/black_shadow/template/product/product.tpl on line 156Notice: Undefined variable: cat_id in /home/madairstil/domains/madairstilius.lt/public_html/oc/catalog/view/theme/black_shadow/template/product/product.tpl on line 156................

In product description i need make to work this function:
if(($cat_id==51) or ($cat_id==53) or ($cat_id==54) or ($cat_id==55) or ($cat_id==68) or ($cat_id==57) or ($cat_id==58) or ($cat_id==59) or ($cat_id==60) or ($cat_id==62) or ($cat_id==69) or ($cat_id==65))
{
echo "<center><div class='size_tab_border'>
<table width='100%' border='1' cellpadding='0' cellspacing='0' class='ismeru_lentele'>
<tbody>
<tr>
<th colspan='7'>

.....
Thanks for help :)

New member

Posts

Joined
Fri Nov 05, 2010 3:37 am

Post by Xsecrets » Fri Apr 15, 2011 3:38 am

there is no easy way to get the category id because opencart allows you to assign a product to multiple categories, so the system has no way to know what category you are expecting when you didn't get to the product page from a category page.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by eric.ag2i » Thu Jun 30, 2011 6:01 pm

same here,

I found the categories id for a product are stored in the following database :
product_to_category

so it must need to read this database with a product id to get all it's categories.

I need to investigate more ;)

EDIT:
I did this quick mod to check if a product match a category, in 3 steps :

1. EDIT /catalog/model/catalog/product.php

BEFORE

Code: Select all

public function getTotalProductsByCategoryId($category_id = 0) {
INSERT

Code: Select all

//MOD START
//check if product is in given category list
public function isProductInCategories($categories_id=array(), $product_id=null) {

$categories_id = implode(',',(array)$categories_id);

$sql = "SELECT 1 FROM " . DB_PREFIX . "product_to_category p2c WHERE p2c.product_id = '".(int)$product_id."' AND p2c.category_id IN (" . $categories_id . ")";

$query = $this->db->query($sql);
				
return (bool)$query->num_rows;
} 
//MOD END


2. EDITcatalog/controller/product/product.php

BEFORE

Code: Select all

$product_info = $this->model_catalog_product->getProduct($product_id);
INSERT

Code: Select all

// MOD START
$catlist = array(51,52,53);
$this->data['isProductInCategories'] = $this->model_catalog_product->isProductInCategories($catlist, $product_id); 
// MOD END   



3. EDIT catalog/view/theme/default/product.tpl

USE ANYWHERE

Code: Select all

<?php
// MOD START
if ( $isProductInCategories )
/* do things here*/
// MOD END
?>
Now simply replace in part2 the catlist by your own categories to match

Code: Select all

//To match multiples cat :
$catlist = array(1,4,5,6);

Code: Select all

//To match one unique cat :
$catlist = array(4);
The bad part is that you need to write categories id "inpage" so it's not very flexible but it will do the trick for me.

Newbie

Posts

Joined
Tue Jan 25, 2011 5:18 pm

Post by karencho » Wed Sep 02, 2015 5:53 am

Hello guys,

I am having the same problem with version 2.0.3.1.
Can someone please provide a solution for opencat version 2.

Thank you
Karen

Newbie

Posts

Joined
Wed Sep 02, 2015 5:46 am

Post by straightlight » Thu Sep 03, 2015 5:06 am

Hi,

one quick solution compared to the long solution above would be by modifying your catalog/model/catalog/product.php file.

Find in the getProducts method:

Code: Select all

$sql = "SELECT p.product_id, (SELECT
Replace with:

Code: Select all

$sql = "SELECT p.product_id, " . (!empty($data['filter_category_id']) ? (!empty($data['filter_sub_category']) ? "`cp`.`path_id` AS `category_id`," : "`p2c`.`category_id` AS `category_id`,") : "") . " (SELECT
This is a simple Ternary operator condition that should either pull the results by path ID or by category ID. You should then be able to pull out the path_id or the category_id from each controllers by using the getProducts method; depending on the filter data that has been entered before calling the method from each controllers.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by karencho » Sun Sep 20, 2015 10:48 pm

Dear straightlight,

Thank you so much for your reply, I am not really a programmer. I don’t really know how to make this work. How do i pull out the path_id or the category_id from each controllers by using the getProducts method??

What I am trying to do is display extra information on product page for only selected categories.

SO far this is what I have

In catalog/controller/product/product.php

Code: Select all

if (isset ($this->request->get['path'])) {
		    $path = $this->request->get['path'];
		    $cats = explode('_', $path);
		    $data['cat_id'] = $cats[count($cats) - 1];
}
In catalog/view/theme/default/template/product/product.tpl

Code: Select all

<?php if (isset($cat_id) && $cat_id == '20'){ ?>
 <!-- My content here… -->
<?php } ?>
This works fine when I view the products through category page.
But when I open the same product via Latest Module I get this error.

Notice: Undefined variable: cat_id in/var/www/vhosts/shop/catalog/view/theme/default/template/product/product.tpl on line 282

I get this error because I guess there is no path ID in the URL when products are opened via latest module.
Could you please tell me how can I fix this issue? Could you provide a simple example so I could follow please??


Thank you for Help
Karen

Newbie

Posts

Joined
Wed Sep 02, 2015 5:46 am

Post by straightlight » Sun Sep 20, 2015 11:37 pm

The cat_id error you're getting is simply because the paragraph condition above has been implied into product.php file only but not into the module/latest.php file. You must ensure to include your conditions into each individual controllers you wish to address.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by karencho » Mon Sep 21, 2015 12:42 am

Hi straightlight,

Sorry I am a little confused here, are you saying that i should include this condition below in the following file
catalog/controller/module/latest.php

Code: Select all

if (isset ($this->request->get['path'])) {
		    $path = $this->request->get['path'];
		    $cats = explode('_', $path);
		    $data['cat_id'] = $cats[count($cats) - 1];
		}
since i am using isset() function in my product.tpl file, the error has been fixed now.

But again the extra information is not appearing on the product page when i view the product page via latest module.

Newbie

Posts

Joined
Wed Sep 02, 2015 5:46 am

Post by straightlight » Mon Sep 21, 2015 12:45 am

The reason why you're not seeing a relative category path on the product page is because below v2.0 releases of OC, the $this->url->link('product/product' also needs to include the &path query within the URL in order for the product page to capture the selected path by the customer.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 337 guests