Page 1 of 1

Limit the number of related products.

Posted: Wed Feb 29, 2012 7:33 pm
by temptious
Is there a way to limit the number of related products on the products page?

I have a product that I want as an up-sell, it's related to around 50% of the products in my store.

However when you visit it's page half of my store is listed in it's related products tab.

I have tried to remove some products from the links panel in Admin, but this removes it from both product pages on the store front.

Is there a simple way to limit these to 8 or even 4 products?

EDIT: OC 1.5 BTW

Re: Limit the number of related products.

Posted: Wed Feb 29, 2012 11:21 pm
by uksitebuilder
edit: catalog/model/catalog/product.php

find (around line 377)

Code: Select all

	public function getProductRelated($product_id) {
		$product_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related pr LEFT JOIN " . DB_PREFIX . "product p ON (pr.related_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pr.product_id = '" . (int)$product_id . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");
		
		foreach ($query->rows as $result) { 
			$product_data[$result['related_id']] = $this->getProduct($result['related_id']);
		}
		
		return $product_data;
	}
change to

Code: Select all

	public function getProductRelated($product_id) {
		$product_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related pr LEFT JOIN " . DB_PREFIX . "product p ON (pr.related_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pr.product_id = '" . (int)$product_id . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' LIMIT 0, 8");
		
		foreach ($query->rows as $result) { 
			$product_data[$result['related_id']] = $this->getProduct($result['related_id']);
		}
		
		return $product_data;
	}
All I did was add LIMIT 0,8 to the SQL which will only grab up to 8 results.

Re: Limit the number of related products.

Posted: Wed Feb 29, 2012 11:26 pm
by temptious
Thanks mate! I'll give that a go.

Re: Limit the number of related products.

Posted: Thu Mar 01, 2012 1:25 am
by Johnathan
A slightly less complicated edit (limiting it to a random selection of 5):

Code: Select all

IN:
/catalog/controller/module/featured.php

BEFORE:
foreach ($products as $product_id) {

ADD:
shuffle($products);
$products = array_slice($products, 0, 5); 

Re: Limit the number of related products.

Posted: Thu Mar 01, 2012 6:50 am
by temptious
Thanks both but I've tried both ways but nothing has changed?

Still getting all related products in the tab.

Any ideas?

Re: Limit the number of related products.

Posted: Thu Mar 01, 2012 11:48 am
by Johnathan
Sorry, I misread your post title...I thought it said Featured Products. (I blame it on the lack of sleep.) Try this:

Code: Select all

IN:
/catalog/controller/product/product.php

AFTER:
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);

ADD:
shuffle($results);
$results = array_slice($results, 0, 5); 

Re: Limit the number of related products.

Posted: Thu Mar 01, 2012 5:57 pm
by temptious
Johnathan wrote:Sorry, I misread your post title...I thought it said Featured Products. (I blame it on the lack of sleep.) Try this:

Code: Select all

IN:
/catalog/controller/product/product.php

AFTER:
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);

ADD:
shuffle($results);
$results = array_slice($results, 0, 5);  
Result! Thanks very much Jonathan.

Now get some sleep!

Re: Limit the number of related products.

Posted: Thu Mar 01, 2012 9:39 pm
by Johnathan
Glad it worked! I'll work on getting some sleep, but my 6-week old daughter decided she will only sleep while being held, which is making it tough. :)

Re: Limit the number of related products.

Posted: Fri Mar 09, 2012 1:45 pm
by Nooneknowsall
uksitebuilder wrote:edit: catalog/model/catalog/product.php

find (around line 377)

Code: Select all

	public function getProductRelated($product_id) {
		$product_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related pr LEFT JOIN " . DB_PREFIX . "product p ON (pr.related_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pr.product_id = '" . (int)$product_id . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'");
		
		foreach ($query->rows as $result) { 
			$product_data[$result['related_id']] = $this->getProduct($result['related_id']);
		}
		
		return $product_data;
	}
change to

Code: Select all

	public function getProductRelated($product_id) {
		$product_data = array();

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related pr LEFT JOIN " . DB_PREFIX . "product p ON (pr.related_id = p.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pr.product_id = '" . (int)$product_id . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' LIMIT 0, 8");
		
		foreach ($query->rows as $result) { 
			$product_data[$result['related_id']] = $this->getProduct($result['related_id']);
		}
		
		return $product_data;
	}
All I did was add LIMIT 0,8 to the SQL which will only grab up to 8 results.
Hello, Thanks for this hint. I limited the related products to 8. But, I will also like the related products to be randomized. Can you help me?

Re: Limit the number of related products.

Posted: Thu Oct 25, 2012 3:22 am
by dydyt
does not work for me

Re: Limit the number of related products.

Posted: Thu Nov 22, 2012 6:31 pm
by MikeSCC
Johnathan wrote:Sorry, I misread your post title...I thought it said Featured Products. (I blame it on the lack of sleep.) Try this:

Code: Select all

IN:
/catalog/controller/product/product.php

AFTER:
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);

ADD:
shuffle($results);
$results = array_slice($results, 0, 5); 
Thank you very much for this.

Re: Limit the number of related products.

Posted: Wed Jun 12, 2013 11:31 pm
by Timmy564
Johnathan wrote:Sorry, I misread your post title...I thought it said Featured Products. (I blame it on the lack of sleep.) Try this:

Code: Select all

IN:
/catalog/controller/product/product.php

AFTER:
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);

ADD:
shuffle($results);
$results = array_slice($results, 0, 5);
Your post is over 1 year old but still helpful. I have just tried this with 1.5.5.1 and seem to be working perfectly, thank you. :)

Re: Limit the number of related products.

Posted: Mon Jul 08, 2013 2:12 pm
by seshalyn
Timmy564 wrote:
Johnathan wrote:Sorry, I misread your post title...I thought it said Featured Products. (I blame it on the lack of sleep.) Try this:

Code: Select all

IN:
/catalog/controller/product/product.php

AFTER:
$results = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);

ADD:
shuffle($results);
$results = array_slice($results, 0, 5);
Your post is over 1 year old but still helpful. I have just tried this with 1.5.5.1 and seem to be working perfectly, thank you. :)
:laugh: <- My reaction when I try something and it works with the first attempt! Also worked for me today using 1.5.4
Thanks Jonathan!

Re: Limit the number of related products.

Posted: Sun May 25, 2014 5:23 pm
by drbyte
A safer one

Drop below code into a new file call it limit_number_of_related_products.xml then drop it in to your VQMOD folder

you can name it whatever you want

Code: Select all

<modification>
	<id>Limit Number Of Related Products</id>
	<version>2.0</version>
	<author>Sam</author>
	<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[shuffle($results); $results = array_slice($results, 0, 6);]]></add>
		</operation>
	</file>
</modification>
Change the number form 6 to whatever you want

Delete vq2-catalog_controller_product_product.php from your vqcache and you are ready to do

Re: Limit the number of related products.

Posted: Tue May 27, 2014 5:39 pm
by draysniro
drbyte wrote:A safer one

Drop below code into a new file call it limit_number_of_related_products.xml then drop it in to your VQMOD folder

you can name it whatever you want

Code: Select all

<modification>
	<id>Limit Number Of Related Products</id>
	<version>2.0</version>
	<author>Sam</author>
	<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[shuffle($results); $results = array_slice($results, 0, 6);]]></add>
		</operation>
	</file>
</modification>
Change the number form 6 to whatever you want

Delete vq2-catalog_controller_product_product.php from your vqcache and you are ready to do
Thank you for this! It's exactly what I was looking for!

Re: Limit the number of related products.

Posted: Sun Jun 07, 2015 1:47 am
by sohoz
My vqcache files are follows,
I didn't find the vq2-catalog_controller_product_product.php, would you please tell me is there any file which one I'm looking for to delete ?