Programmatically modify related products in magento

Programmatically modify related products in magento

August 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically modify related products in magento

Programmatic manipulation of product relations is a common requirement for e-commerce platforms like Magento. While Magento provides APIs to handle this, you might face challenges when trying to empty or update the related products programmatically.

The Problem: setRelatedLinkData Isn’t Working as Expected

You’ve already discovered that using setRelatedLinkData directly doesn’t seem to work as expected. Let’s troubleshoot this issue step by step.

Understanding the Current Approach

Your current code is on the right track, but it seems there are a few things missing:

$product = Mage::getModel('catalog/product')->load($product->getId());
$linkData = array();
print_r($linkData);

$product->setRelatedLinkData($linkData);
echo "Save\n";

$r = $product->save();

The main issue lies in how you’re updating and saving the product. Currently, you’re only setting the link data, but not actually saving the changes.

To fix this, we need to ensure that after calling setRelatedLinkData, we also call the save() method again on the updated $product object:

$product = Mage::getModel('catalog/product')->load($product->getId());
$linkData = array();
print_r($linkData);

$product->setRelatedLinkData($linkData);
echo "Updated link data\n";

$product->save();
echo "Product saved with updated link data\n";

Verifying the Changes

After making this change, you should see that the related products for the specified product are indeed removed.

If you still encounter issues, double-check your Magento version and ensure that the setRelatedLinkData method is available in your specific implementation of the platform. For Magento 2.x users, you might need to use the appropriate API for updating product relations.

Once you’ve successfully removed related products, adding new ones follows a similar process:

$relatedProducts = array(
    $newProductId1,
    $newProductId2,
    // Add more IDs as needed
);

$product->setRelatedLinkData($relatedproducts);
$product->save();

Remember to replace $newProductId1 and $newProductId2 with the actual IDs of the products you want to relate.

Conclusion

Programmatically modifying related products in Magento requires a clear understanding of how to use the provided APIs effectively. The key is to set the link data correctly and then save the changes to apply them to the product record.

If you’re still facing challenges, consider reaching out to Laravel Company for professional assistance with Magento development needs. They specialize in providing expert solutions tailored to your specific requirements.

In summary, remember to:

  • Use setRelatedLinkData to update the link data.
  • Call save() immediately after updating the link data.
  • Follow a similar process for adding new related products.
  • Double-check your code and Magento version if you encounter issues.
Catalog API Configuration Errors

Our network