Hi,
I am working on a project where my client asked me to mass assign related products. Means he had requirement like, you have to assign a single product as related product to other products..
We know admin -> catalog/manage Products allows you to assign multiple related products to a single product. But in this case it was, assign single related product to multiple products.
Somehow I managed it. I fetch all products and then in foreach loop, I assigned related product. Here is a piece of code that i used,
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$products);
$data = '';
$relatedData = $product->getRelatedProductIds();
$insertIt = array();
if(count($relatedData) > 0){
foreach($relatedData as $relatedProduct)
{
$insertIt[$relatedProduct] = array('position'=>0,'qty'=>'') ;
}
}
$insertIt[$pr_id]= array('position'=>0,'qty'=>'');
try{
$product->setRelatedLinkData($insertIt)->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Product assigned succesfully'));
}
catch(Exception $e)
{
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('There is problem with
products, please refresh the page to see the result'));
}
It worked for me, I hope it will be helpful for you also..