3

I have a magento store in which we already created more than 300 products. The thing is each 10 to 15 products are more than 80% same name format.

Eg:

Embroidered Vine - 12
Embroidered Vine - 13
Embroidered Vine - 14
Embroidered Vine - 15
Embroidered Vine - 16
Embroidered Vine - 17

Here right now am showing the other related items by entering those items in description field. Instead i want to run a script and the script will needs to add the related products automatically because product names are similar except the last two characters. Please can some one tell me how i can setup related products in magento by code automatically.

Is there any function like setRelatedItems() to the product in magento.

Am using magento 1.4.2

Elamurugan
  • 3,204
  • 13
  • 59
  • 104

2 Answers2

5

You could just do some raw SQL queries for this. I have worked on a dataload that mass imports products into magento so I know how to do it.

What you could do is the following:

SELECT DISTINCT cpev.entity_id

FROM catalog_product_entity_varchar cpev

WHERE value LIKE 'Embroidered Vine%'

Here you now have all the entity id's of the products that start with 'Embroidered Vine' as title. This result can be stored in an array let's say $result.

Then you have to make a double loop (for each Embroidered Vine product you must add all the other Embroidered Vine products as related product)

First make a copy of all the 'Embroidered Vine' products in another array for the foreach loop (this may not be needed but still just do it).

$copy = $result; // Where result is the result of the query (= the entity_id's)
foreach($result as $main_product){ //Each 'Embroidered Vine' product
  foreach($copy as $related_product){
    if($main_product["entity_id"] == $related_product["entity_id"])
      continue; //We do not want to add the same products as related product

    // Insert related product
    mysql_query("INSERT INTO catalog_product_link (product_id,linked_product_id,link_type_id) VALUES ($main_product["entity_id"],$related_product["entity_id"],1)");
  }
}

If you need more help just add a comment and I will see what I can do ;)

I hope this has helped you.

Regards, Kenny

Kenny
  • 5,350
  • 7
  • 29
  • 43
0

You can pass product id for which you require related product. Here you can make loop of product and using getRelatedProductIds() you can achieve this

E.g You need related product for a particular product (Say $_product)

You can get related product ids by

  $_product->getRelatedProductIds()

You can see array of ids by :

  print_r($_product->getRelatedProductIds()); 

I hope this will help you.

Regards, Kamesh J

Kamesh Jungi
  • 6,203
  • 6
  • 39
  • 50