Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set related products By code for each product in magento

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

like image 510
Elamurugan Avatar asked Dec 06 '25 01:12

Elamurugan


1 Answers

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

like image 60
Kenny Avatar answered Dec 08 '25 00:12

Kenny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!