<?php
set_time_limit(0);
ini_set(‘memory_limit’,’1024M’);
require_once ‘../../app/Mage.php’;
Mage::app();
$sku=’12345’; //some Sku
$product = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’,$sku);
if($product){
$sRelatedProducts = “123456,123”;
$aRelatedProducts = explode(‘,’, $sRelatedProducts); // or other way to get the array of related product sku
$aParams = array();
$nRelatedCounter = 1;
$aProduct = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’, $sku);
$aMainProduct = Mage::getModel(‘catalog/product’);
$aMainProduct->load($aProduct[‘entity_id’]);
foreach($aRelatedProducts as $sSku)
{
$aRelatedProduct = Mage::getModel(‘catalog/product’)->loadByAttribute(‘sku’, $sSku);
$aParams[$aRelatedProduct[‘entity_id’]] = array(
‘position’ => $nRelatedCounter
);
$nRelatedCounter++;
}
$product->setRelatedLinkData($aParams);
$product->save();
}
echo “Great!!”;
?>