Subesh Pokhrel

Magento Developers Blog

Creating Product Links in Magento Using Custom Query

I’ve already made a post in this blog about Adding Related Product and other links to Product in Magento . But when I used the method to creating links among product’s, while importing very large number of products and creating large number of product links, it took a lot of time and resource. So I thought why not give it a try by direclty creating a link using SQL? And YES! it worked. Below is the code how I created those links using Custom SQL. But I have to tell you that the former methods looks organized, this is just a work around to a situation. You might know what I am trying to say! [source language=”php”] $resource = Mage :: getSingleton( ‘core/resource’ ); $read= $resource -> getConnection( ‘core_read’ ); $write= $resource->getConnection(‘core_write’); $linkTable=$resource->getTableName(‘catalog/product_link’); // Creating Upsell Product link $write->query("INSERT into $linkTable SET product_id=’".$productId."’, linked_product_id=’".$linkProduct."’, link_type_id=’".Mage_Catalog_Model_Product_Link::LINK_TYPE_UPSELL."’ "); // Creating Related Product link $write->query("INSERT into $linkTable SET product_id=’".$productId."’, linked_product_id=’".$linkProduct."’, link_type_id=’".Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED."’ "); // Creating Crosssell Product Link $write->query("INSERT into $linkTable SET product_id=’".$productId."’, linked_product_id=’".$linkProduct."’, link_type_id=’".Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL."’ "); [/source] Happy Coding!