Post by myshadowself » Mon Jun 20, 2011 8:08 pm

I've created a mod to automatically relate products by Brand/Manufacturer when no other related products are defined - which is great for an Up-sell or a Cross-sell and would even aid relevancy as far as SEO goes.

This acts as a fallback if you can't be bothered to relate products together. I've just posted about this mod on my blog, but thought I'd share it in the forums too.

This is only tested on Version 1.4.9.5.

Open ROOT/catalog/controller/product/product.php

Find:

Code: Select all

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

Code: Select all

if(count($results)<1 && (int)$this->data['product_info']['manufacturer_id'] > 0){
$results = $this->model_catalog_product->getProductRelatedByManufacturer($this->data['product_info']['manufacturer_id'],$this->request->get['product_id']);
 }

Next, open ROOT/catalog/model/catalog/product.php

find:

Code: Select all


public function getProductRelated($product_id) {

 $product_data = array();

 $product_related_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "'");

 foreach ($product_related_query->rows as $result) {

 $product_query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, ss.name AS stock, (SELECT AVG(r.rating) FROM " . DB_PREFIX . "review r WHERE p.product_id = r.product_id GROUP BY r.product_id) AS rating FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.product_id = '" . (int)$result['related_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.date_available <= NOW() AND p.status = '1'");                          if ($product_query->num_rows) {

 $product_data[$result['related_id']] = $product_query->row;

 }

 }

 return $product_data;

 }

Directly AFTER that, add:

Code: Select all


public function getProductRelatedByManufacturer($manufacturer_id,$product_id) {

 $product_data = array();

 $product_related_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product WHERE manufacturer_id = '" . (int)$manufacturer_id . "' LIMIT 0,9");

 foreach ($product_related_query->rows as $result) {

 $product_query = $this->db->query("SELECT DISTINCT *, pd.name AS name, p.image, m.name AS manufacturer, ss.name AS stock, (SELECT AVG(r.rating) FROM " . DB_PREFIX . "review r WHERE p.product_id = r.product_id GROUP BY r.product_id) AS rating FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) LEFT JOIN " . DB_PREFIX . "manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN " . DB_PREFIX . "stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.product_id = '" . (int)$result['product_id'] . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND ss.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.date_available <= NOW() AND p.status = '1'");                          if ($product_query->num_rows && $result['product_id'] != $product_id) {

 $product_data[$result['product_id']] = $product_query->row;

 }

 }

 return $product_data;

 }

Save both and upload to the site.

You now have a built in system that automatically adds products of the same brand or manufacturer but ONLY IF you haven't related products already. If there are no other similar branded products, it should fail silently in much the same way as if there were no related products.

Hopefully someone will find this useful

I'm thinking of adding the functionality to try related by category if there are no brands, but I'm not so sure it's worth it really. Let me know what you think.

Feedback welcome

Ronald Laughton is the worst referee in Rugby League


New member

Posts

Joined
Tue Jun 07, 2011 8:59 pm

Post by stonehinged » Mon Jul 18, 2011 10:50 am

Gosh myshadowself, I think it's a fantastic mod and idea. I'm looking for any way possible to help me get more efficient with related products. Unfortunately, I've tried your mod (I'm using 1.4.7 with some other mods) and I've tried my best to use your suggestions above but it's not working for me.

If I could get this working somehow, I'd love to be your guinea pig for"related by category". That would the cat's meow as far as my store is concerned. I guess the only issue might be if products are associated to multiple categories. So for that reason, it would be nice as a possible consideration for that particular mod to be able to select the category you want to use (if an admin mod it could even be a multiple select).

I'm going to keep playing around with your code and hope I can get it to work for my store.

Thanks for a great contribution, even though it won't work for me at the moment :)

STONEHINGED
Handcrafted, Semi-Precious Jewelry
http://www.stonehinged.com/


User avatar
Active Member

Posts

Joined
Wed Aug 18, 2010 9:10 am
Location - US
Who is online

Users browsing this forum: No registered users and 48 guests