Thursday, 24 November 2011

Adding related products to the Magento by code / script


$sku='f343434';  //some Sku
$product = $_product->loadByAttribute('sku', $sku);

if($product)
{
$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', $sProduct);
$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();
}

7 comments :

  1. I dugg some of you post as I thought they were very useful very helpful

    ReplyDelete
  2. Sup there administrator, I absolutely wished to give a quick remark to actually mention that I preferred your specific story. Thanks!

    ReplyDelete
  3. Thanks for an amazing submit, will study one's others content.

    ReplyDelete
  4. it's worth a read. thank you for sharing so much information!

    ReplyDelete
  5. Hmm...

    Thank you for sharing superb informations. Your internet site is really cool. I’m impressed by the details which you have on this internet site. It reveals how nicely you perceive this subject. Bookmarked this site page, will come back for extra articl...

    ReplyDelete
  6. nice post!http://www.agraupe.net

    ReplyDelete
  7. Thanks for any other fantastic post. The place else may just anyone get that kind of info in such a perfect approach of writing? I have a presentation subsequent week, and I am on the look for such info.

    ReplyDelete