1

i need to modify all products description by sku : i have csv file with two column first column define sku and twice define description so how i can do that this my script

      <?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
ini_set('memory_limit','1024M');

$csv                = new Varien_File_Csv();
$data               = $csv->getData('comment.csv'); //csv
array_shift($data);
  $collection = Mage::getModel('catalog/product')
    ->getCollection();
foreach ($collection as $product) {
    try{
        $product
        ->setDescription($data[1])
        ->setShortDescription($data[1]) 
        ->addFieldToFilter('sku',$data[0]);
        $product->save();
        echo "done";
    }
    catch(Exception $e){
   echo Mage::log($e->getMessage()) . " Error"."</br>";
 }
}
?> 
  • could you please share your CSV array i.e print_r($data) ? – Pawan Apr 22 '18 at 3:00
  • did you try my solution ? – Pawan Apr 25 '18 at 18:29
0

You can use below code for update attribute:

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
ini_set('memory_limit','1024M');

$csv = new Varien_File_Csv();
$csv->setDelimiter(',');//this is important to separate SKU with description

$data = $csv->getData('comment.csv'); //csv
for($i=0; $i<count($data); $i++)
    {
try{
    $sku = $data[$i][0];
    $product_id = Mage::getModel("catalog/product")->getIdBySku($sku);
    $storeId = Mage::app()->getStore()->getStoreId();

    if ($product_id) {

        $description = 'description';
        $shortDescription = 'short_description';
        $updater = Mage::getSingleton('catalog/product_action');
        $updater->updateAttributes(array($product_id), array($description => $data[$i][1],$shortDescription => $data[$i][1]), $storeId);
        echo 'Updated Product with SKU of ' . $sku;
        echo '<br>';
    }
    else {
        echo 'Product not found with SKU of ' . $sku;
        echo '<br>';
    }
}
catch(Exception $e){
    echo Mage::log($e->getMessage()) . " Error"."</br>";
}
}

Note: Advantage of above method is no need to product collection and no more load product!. Before running script, please print your $data array and it should look like:

Array
 (
    [0] => Array
        (
            [0] => ab<!--sku-->
            [1] => test
        )

    [1] => Array
       (
            [0] => ba<!--sku-->
            [1] => again test
        )

)
| improve this answer | |
0

You have to go through the data array not through product collection, Please try this code, it should be work

<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
ini_set('memory_limit','1024M');

$csv                = new Varien_File_Csv();
$data               = $csv->getData('comment.csv'); //csv
array_shift($data);
foreach ($data as $item) {
    $sku = $item[0];
    $description = $item[1];
    $_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    $_product->setDescription($description);
    $_product->setShortDescription($description);
    $_product->save();
}
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.