Add related products programmatically Magento

In this post we will show you Add related products programmatically Magento, hear for Add related products programmatically Magento we will give you demo and example for implement.

How to related products Add programmatically Magento

we will show you How to Add related products to the Magento by code or script. For related products Add programmatically you need this code.

<?php
	// if call is come form external file
	set_time_limit(0);
	ini_set('memory_limit','1024M');
	require_once '../app/Mage.php';
	Mage::app();

	 // your Sku	
	$set_sku = 'onlinecode'; 
	$get_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$set_sku);

	if($get_product)
	{
		// add sku in string or other way to 
		// get the array of related product sku
		$related_products = "5689,965";
		$related_products_array = explode(',', $related_products);  
		

		$params = array();
		$related_counter = 1;
		$Product    = Mage::getModel('catalog/product')->loadByAttribute('sku', $set_sku);
		$main_product = Mage::getModel('catalog/product');
		$main_product->load($Product['entity_id']);

		foreach($related_products_array as $sku_value)
		{
			$related_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku_value);

			$params[$related_product['entity_id']] = array(
			'position'     => $related_counter
			);

			$related_counter++;
		}

		$get_product->setRelatedLinkData($params);
		$get_product->save();
	}

	echo "Add related products programmatically updated successfully ";

?>

Hope this code and post will helped you for implement Add related products programmatically Magento. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org