Example CSV :
SKU,Related SKUs
HG1126879,"HG1235845, HG1235852, hello123"
HG1201847,"HG1201862, HG1201771"
<?php
set_time_limit(0);
ini_set('memory_limit','1024M');
require_once('app/Mage.php');
Mage::app();
//echo '<pre>';
$csvpath = getcwd().'/related.csv';
$row = 1;
if(($handle = fopen($csvpath, "r")) !== FALSE)
{
while($data = fgetcsv($handle,0,","))
{
if($row!=1)
{
//print_r($data);
$sku = $data[0];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product)
{
$sRelatedProducts = $data[1];
$aRelatedProducts = explode(',', $sRelatedProducts);
$aParams = array();
$nRelatedCounter = 1;
foreach($aRelatedProducts as $sSku)
{
$aRelatedProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', trim($sSku));
if($aRelatedProduct)
{
$aParams[$aRelatedProduct['entity_id']] = array('position' => $nRelatedCounter);
$nRelatedCounter++;
}
else
{
$msg = "CSV Row - $row : Related Product with SKU ".trim($sSku)." not found";
Mage::log($msg, null, 'add_related.log');
}
}
//print_r($aParams);
$product->setRelatedLinkData($aParams);
$product->save();
}
else
{
$msg = "CSV Row - $row : Product with SKU $sku not found";
Mage::log($msg, null, 'add_related.log');
}
}
$row++;
}
echo "Add Related Products script executed successfully.";
echo "<br/><br/>CSV has Records : ".($row-2);
}
SKU,Related SKUs
HG1126879,"HG1235845, HG1235852, hello123"
HG1201847,"HG1201862, HG1201771"
<?php
set_time_limit(0);
ini_set('memory_limit','1024M');
require_once('app/Mage.php');
Mage::app();
//echo '<pre>';
$csvpath = getcwd().'/related.csv';
$row = 1;
if(($handle = fopen($csvpath, "r")) !== FALSE)
{
while($data = fgetcsv($handle,0,","))
{
if($row!=1)
{
//print_r($data);
$sku = $data[0];
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if($product)
{
$sRelatedProducts = $data[1];
$aRelatedProducts = explode(',', $sRelatedProducts);
$aParams = array();
$nRelatedCounter = 1;
foreach($aRelatedProducts as $sSku)
{
$aRelatedProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', trim($sSku));
if($aRelatedProduct)
{
$aParams[$aRelatedProduct['entity_id']] = array('position' => $nRelatedCounter);
$nRelatedCounter++;
}
else
{
$msg = "CSV Row - $row : Related Product with SKU ".trim($sSku)." not found";
Mage::log($msg, null, 'add_related.log');
}
}
//print_r($aParams);
$product->setRelatedLinkData($aParams);
$product->save();
}
else
{
$msg = "CSV Row - $row : Product with SKU $sku not found";
Mage::log($msg, null, 'add_related.log');
}
}
$row++;
}
echo "Add Related Products script executed successfully.";
echo "<br/><br/>CSV has Records : ".($row-2);
}
This comment has been removed by the author.
ReplyDeleteGreat Read! I am impressed on how you make your article easy to understand. I'll come back for more :D
ReplyDeleteJaps Buidon is a Social Media Specialist and belongs to a team of Magento Developer in Florida. For more tutorial and tips you can follow him here -> alwaysopencommerce.com
Instead of $product->setRelatedLinkData($aParams);
ReplyDelete$product->save();
I used : Mage::getResourceModel('catalog/product_link')->saveProductLinks(
$product, $aParams, Mage_Catalog_Model_Product_Link::LINK_TYPE_RELATED
);
It worked fine for me.
Anyway thanks