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
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
edit: catalog/model/catalog/product.php
find (around line 377)
change to
All I did was add LIMIT 0,8 to the SQL which will only grab up to 8 results.
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;
}
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;
}
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);
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.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);
Now get some sleep!
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?uksitebuilder wrote:edit: catalog/model/catalog/product.php
find (around line 377)change toCode: 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; }
All I did was add LIMIT 0,8 to the SQL which will only grab up to 8 results.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; }
Thank you very much for this.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.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);

Occasional Furniture | Dining Sets | Bedroom
Sofas | Chandeliers | Vases
Timmy564 wrote: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.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);

Thanks Jonathan!
I <3 Opencart, but I HATE PHP!
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
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
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>
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!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
Change the number form 6 to whatever you wantCode: 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>
Delete vq2-catalog_controller_product_product.php from your vqcache and you are ready to do
Who is online
Users browsing this forum: No registered users and 144 guests