Post by orangebox321 » Mon Sep 24, 2012 5:19 pm

below is a segment of code for a VQ Mod to auto populate related products which works fine but problem i have is my site has a main category for General Food with several sub category's.

as this code pulls products directly from main categories the products related could be any product from within its main category.

is there a way to change it to tell it to only pull info from sub category rather than the parent category so only products directly related show.

ex: i having baking goods sub cat so flour is a product in there along with other types of flour but this mod sets related products to tuna in a tin as this is in the main general food category but under tinned goods.

any help trying to alter code to only pick within sub category would be appreciated

Code: Select all

//related by category
				if(count($results)<3){//change to suit number of products to display
				$temp = $this->model_catalog_product->getProductRelatedByCategory($this->request->get['product_id'],count($results));
				foreach($temp as $t){               
				 if(!empty($t)){                 
				    $results[] = $t;                
					}        
			    }        
			}    

New member

Posts

Joined
Mon Mar 26, 2012 1:11 am

Post by opcrat » Wed Sep 26, 2012 4:59 am

Exclude your main category from catalog/model/catalog/product.php's getProductRelatedByCategory method.

Most Useful Extensions || Most Selling Extensions
Opencart Development || Opencart Designing || eCommerce Project

✔ Join more than 5000 Happy Opencart Store Owners enjoying the extensions designed and developed by Opcrat, Opencart Partner.


User avatar
Active Member

Posts

Joined
Wed Dec 21, 2011 6:54 am

Post by orangebox321 » Wed Sep 26, 2012 9:30 am

opcrat wrote:Exclude your main category from catalog/model/catalog/product.php's getProductRelatedByCategory method.

not sure what you mean i can see that its only pulling items from main catergory as set by code to "getProductRelatedByCategory" and i need it to only do it from the sub_category but not 100% how i would mod the code to change this out, php is not my strong point.

New member

Posts

Joined
Mon Mar 26, 2012 1:11 am

Post by opcrat » Wed Sep 26, 2012 9:55 am

Can you please copy here getProductRelatedByCategory method from catalog/model/catalog/product.php so that we can try to help you.

Most Useful Extensions || Most Selling Extensions
Opencart Development || Opencart Designing || eCommerce Project

✔ Join more than 5000 Happy Opencart Store Owners enjoying the extensions designed and developed by Opcrat, Opencart Partner.


User avatar
Active Member

Posts

Joined
Wed Dec 21, 2011 6:54 am

Post by orangebox321 » Wed Sep 26, 2012 5:00 pm

i looked in that file and there was no code for getProductRelatedByCategory i would post all of the code here but its a tad long only code i can find with getProductRelatedByCategory is in the xml for the VQ mod and there is code located catalog/controller/product/product.php for getrelatedby

Code: Select all

<file name="catalog/controller/product/product.php">
		<operation>
			<search position="after"><![CDATA[$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);]]></search>
			<add><![CDATA[
			       				
				//related by category
				if(count($results)<3){//change to suit number of products to display
				$temp = $this->model_catalog_product->getProductRelatedByCategory($this->request->get['product_id'],count($results));
				foreach($temp as $t){               
				 if(!empty($t)){                 
				    $results[] = $t;                
					}        
			    }        
			}    
			
			//related by manufacturer
				   if(count($results)<3 && (int)$this->data['product_info']['manufacturer_id'] > 0){//change to suit number of products to display 
				   $temp = $this->model_catalog_product->getProductRelatedByManufacturer($this->data['product_info']['manufacturer_id'],$this->request->get['product_id'],count($results));                   foreach($temp as $t){
				     if(!empty($t)){                    
					    $results[] = $t;               
						 }          
				    }        
				}           
			   
			   //related by randomisation
			   if(count($results)<3){//change to suit number of products to display
			    $temp = $this->model_catalog_product->getProductRelatedByNothing($this->request->get['product_id'],count($results));
	            foreach($temp as $t){              
				  if(!empty($t)){                
				      $results[] = $t;                
					  }            
			}       
		}     
		]]></add>
		</operation>
	</file>
	
		<file name="catalog/model/catalog/product.php">
		<operation>
			<search position="after" offset="4"><![CDATA[$product_data[$result['related_id']] = $this->getProduct($result['related_id']);]]></search>
			<add><![CDATA[
									
					 public function getProductRelatedByCategory($product_id,$num_results) {
					//return array();
					$product_data = array();
					 $num = 3 - $num_results;//change to suit number of products to display 
					 $getCat = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$getCat->row['category_id'] . "' AND product_id != '" . (int)$product_id . "' ORDER BY RAND() LIMIT 0,".$num."");                       
					 foreach ($query->rows as $result) {
					     $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
				   }   
				    return $product_data;
				} 
				
				public function getProductRelatedByManufacturer($manufacturer_id,$product_id,$num_results) {
				//return array();
				 $product_data = array();
				  $num = 3 - $num_results;//change to suit number of products to display
				   $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE manufacturer_id = '" . (int)$manufacturer_id . "' AND product_id != '" . (int)$product_id . "' LIMIT 0,".$num."");
				   foreach ($query->rows as $result) {
				      $product_data[$result['product_id']] = $this->getProduct($result['product_id']);  
				}     
				    return $product_data;
					
					}
				    
					public function getProductRelatedByNothing($product_id,$num_results) {
					 $product_data = array();
					 $num = 3 - $num_results;//change to suit number of products to display 
					   $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE product_id != '" . (int)$product_id . "' ORDER BY RAND() LIMIT 0,".$num."");
					   
					   foreach ($query->rows as $result) {
					       $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
				    }    
					 return $product_data;
			} ]]></add>
		</operation>
	</file>
	
	

	
</modification>

New member

Posts

Joined
Mon Mar 26, 2012 1:11 am

Post by opcrat » Thu Sep 27, 2012 1:53 am

Replace below Query,

Code: Select all

$getCat = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'"); 
With,

Code: Select all

$getCat = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' AND category_id != Your Parent Category ID Which You Wants To Exclude "); 

Most Useful Extensions || Most Selling Extensions
Opencart Development || Opencart Designing || eCommerce Project

✔ Join more than 5000 Happy Opencart Store Owners enjoying the extensions designed and developed by Opcrat, Opencart Partner.


User avatar
Active Member

Posts

Joined
Wed Dec 21, 2011 6:54 am

Post by aus10 » Mon Jul 22, 2013 11:17 am

Can you give an example of how to do this with multiple categories being excluded. I can't even tell if one is working (if I am doing it right) because other categories / subcategories would be having the same effect, as well as manufacturers. Also, how would you remove the manufacturers and random sections of this script without breaking it, I had no luck.

Here is the line referred to above:

Code: Select all

					 $getCat = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$getCat->row['category_id'] . "' AND product_id != '" . (int)$product_id . "' ORDER BY RAND() LIMIT 0,".$num."");                       
If you need anything to help with the manufacturer issue please LMK.

New member

Posts

Joined
Mon Jul 22, 2013 10:48 am

Post by aus10 » Mon Jul 22, 2013 12:21 pm

Ok well I got it working for 1 category, unfortunately I have about 8 more I need to exclude and a few subcategories as well. Also, I think i managed so get it to work for the manufacturer i needed excluded as well.

New member

Posts

Joined
Mon Jul 22, 2013 10:48 am

Post by aus10 » Tue Jul 23, 2013 4:34 am

I'm getting this error now:

Notice: Undefined index: product_info in /vqmod/vqcache/vq2-catalog_controller_product_product.php on line 416

Here is the modified category line:

Code: Select all

$getCat = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' AND category_id != 59 ");     $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$getCat->row['category_id'] . "' AND product_id != '" . (int)$product_id . "' ORDER BY RAND() LIMIT 0,".$num."");                       
And same thing done to manufacturer line of code:

Code: Select all

$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE manufacturer_id = '" . (int)$manufacturer_id . "' AND product_id != '" AND manufacturer_id != 11 . (int)$product_id . "' LIMIT 0,".$num."");
Still havne't figured out how to add multiples of categories, or manufacturers, or how to omit a subcategory.

FYI I did try clearing the cache and the error reappears after a couple refreshes.

New member

Posts

Joined
Mon Jul 22, 2013 10:48 am

Post by aus10 » Tue Jul 23, 2013 5:11 am

Line 416 in the cache file corresponds to this line in the XML, which i haven't changed.

Code: Select all

if(count($results)<3 && (int)$this->data['product_info']['manufacturer_id'] > 0){//change to suit number of products to display 

New member

Posts

Joined
Mon Jul 22, 2013 10:48 am

Post by aus10 » Tue Jul 23, 2013 5:13 am

If there is a better extension for related products I am all ears. I don't see how everyone who uses opencart is doing this by hand or messing with this buggy script. Please reply.

New member

Posts

Joined
Mon Jul 22, 2013 10:48 am

Post by aus10 » Tue Jul 23, 2013 1:05 pm

bump

New member

Posts

Joined
Mon Jul 22, 2013 10:48 am

Post by wilek666 » Tue Mar 10, 2015 1:46 am

if you want to have related product from same subcategory replace this query in extension:

Code: Select all

$getCat = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
with this:

Code: Select all

$getCat = $this->db->query("SELECT product_id, min(category_id) as category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' AND category_id not in (SELECT distinct parent_id as category_id FROM oc_category)");

http://www.ortopediko.pl
http://www.pszczelafarma.pl


User avatar
New member

Posts

Joined
Sun Dec 07, 2014 2:26 am
Location - Warsaw, Poland

Post by srdrltn » Mon Mar 23, 2020 7:30 pm

wilek666 wrote:
Tue Mar 10, 2015 1:46 am
if you want to have related product from same subcategory replace this query in extension:

Code: Select all

$getCat = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "'");
with this:

Code: Select all

$getCat = $this->db->query("SELECT product_id, min(category_id) as category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' AND category_id not in (SELECT distinct parent_id as category_id FROM oc_category)");
Sorry for writing to this old topic

But is it possible to combine these extension for 2.1x?
I checked codes but couldnot figured it out

Newbie

Posts

Joined
Mon Dec 15, 2014 5:37 pm
Who is online

Users browsing this forum: No registered users and 216 guests