Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email

Magento Forum

How You Can Assign Related Product Programmitically
Jr. Member
Total Posts:  11
Joined:  2012-11-25

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’=>”);
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..

Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top