READ CSV -- getData()

To read a csv file the file has to be in Magento root. For security reasons, common approach to upload or save  our csv file are:

media/import

media/wysiwyg

var/import

If needed depending on the given task, the csv can be saved other path.

Once our csv is uploaded or saved in a given path, now it’s time to read. To read a CSV file create a new instance of the Varien_File_Csv() class and pass CSV file path into the getData() method.

The following code example shows how to read a csv file with 5 column headers and to update corresponding product tier prices for a given website id

product_id,website_id,customer_group,price,price_qty
<?php
//ensure the csv has 5 columns
//product_id,website_id,customer_group,price,price_qty
$file = 'example.csv';
$path = Mage::getBaseDir('var') . DS . 'export' . DS . $file;
$csv = new Varien_File_Csv();
$data = $csv->getData($path);

for ($i=0; $i<count($data) ; $i++) {
    Mage::getModel("catalog/product")->load($data[$i][0])
        ->unsTierPrice()
        ->save()
        ->setTierPrice(array(
            array(
                'website_id'    => $data[$i][1],
                //'all_groups'    => $data[$i][0],
                'cust_group'    => $data[$i][2],
                'price'         => $data[$i][3],
                'price_qty'     => $data[$i][4],
            ),
            array() // another tier, etc
        ))
        ->save();

}

 

WRITE CSV -- saveData()

To write data to a CSV is basically the opposite of what’s above. Using our CSV object call the method saveData() and pass in your file path and array. Our array needs to be 2-dimensional. The following code example shows how to get all product ids for a given category id and save those product ids in $productIds array.

Now, for each product id save in $productData associative array and pass this array to our $csvData array. Finally just do $csv->saveData($path, $csvData).

<?php

$categoryid= '33';
$productCollection = Mage::getModel('catalog/category')->
load($categoryid)->getProductCollection();

if (isset($productCollection)) {
    foreach($productCollection as $productObject) {
        $productIds[] = $productObject->getId();
    }
}

    $file = 'example.csv';
    $path = Mage::getBaseDir('var') . DS . 'export' . DS . $file;
    $csv = new Varien_File_Csv();
    $csvData = array();
    //$products = Mage::getModel('catalog/product')->load($productId);
    $productData = array();

    foreach ($productIds as $productId) {
        $productData['id'] = $productId;
        $csvData[] = $productData;

    }

    $csv->saveData($path, $csvdata);

 

Afer saving the file, the csv will  have NO HEADER and consists of one column.

We can write more than one column. Assuming we have got product name for each of our product id. To write this, we need to add another “key” to our $productData associative array like below:

foreach ($productIds as $productId) {
        $productData['id'] = $productId;
        $productData['name'] = $productName;
        $csvData[] = $productData;

    }

That’s all!

Categorized in: