1

I wrote a code to add product description from sku. Now script works fine but all formatting seems to be lost as in csv and description added to product is partial (seems it removes string after any special character).

My code:

require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
if (($handle = fopen("Witedescriptions.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $desc =$data[1];
        $sku = $data[2];
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
        $product->setDescription($desc);
        $product->save();
    }
    fclose($handle);
}

Csv file struture

Sno. | Description | Sku

on sample data for description

Test this is dummy data abd’s base is of a slim yellow gold halo that easily fits on a finger. 

list here 
•   list 1
•   list 2

When I ran script, product has below data only:

Test this is dummy data abd

1 Answer 1

0

Below is code using Magento's native methods to parse your CSV. I do notice in your function you are indicating that a comma is your field delimiter but in the file structure sample you show a | symbol as your delimiter.

If the pipe symbol is actually your delimiter, then below you would use $processor->streamReadCsv('|'); as the default delimiter for that function is a comma. Additionally, if your fields are not enclosed in double quotes, you should enter null as the second argument because " is the default field enclosure. So it would be like this $processor->streamReadCsv('|', null);

Finally, if your fields are not enclosed in double quotes, it is strongly recommended that you do enclose them in double quotes. Ideally, your file structure should look more like this:

"Sno.","Description","Sku"

-

$file      = 'path/to/your/file.csv';
$processor = new Varien_Io_File();
$processor->streamOpen($file, 'r');

while($row = $processor->streamReadCsv()) {
    // do whatever you need to do here with the csv row data
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $row[2]);
    $product->setData('description',$row[1]);
    $product->save();
}
$processor->streamClose();

Also, notice that I used $product->setData('description',$row[1]); instead of $product->setDescription($row[1]);. Thats because when you are parsing large amounts of data, you would just be giving Magento more work to do because it has to do some converting of the non-existent method setDescription('content') into setData('description','content')


Update to answer

After trying with your file specifically, it appears that you need to UTF-8 encode your data. Additionally, ALL fields need to be surrounded by double quotes, especially since your data is not contained on one line but instead you have one product taking up several lines in the text file.

If you are using microsoft excel that wont work because excel strips quotes. You can use a free product like OpenOffice.org Calc to ensure that the " double quotes don't get stripped out of your csv. With that being said, here is the code that worked for me with your file specifically.

<?php 

require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$file      = 'path/to/your/file.csv';
$processor = new Varien_Io_File();
$processor->streamOpen($file, 'r');

while($row = $processor->streamReadCsv()) {
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $row[2]);
    $product->setData('description',utf8_encode($row[1]));
    $product->save();
}
$processor->streamClose();
11
  • hey seem does i have to escape some char i tried your code with my descrition set as "Gold’s price in USA" but in admin product i just got "Gold"
    – gaurav
    Commented Aug 12, 2016 at 7:48
  • can you post the file or email it to me? It sounds like something may be wrong with the file itself. Commented Aug 12, 2016 at 12:10
  • can you tell email
    – gaurav
    Commented Aug 12, 2016 at 12:29
  • g17owner at gmail dot com Commented Aug 12, 2016 at 12:31
  • i hope you understand it, trying to keep the spambots away Commented Aug 12, 2016 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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