1

I am working on one custom module where i imported products from csv successfully.But i am doing like first field must be sku and second should be name.But the issue is if someone change the sequence then it creates issues.Is there any way i can map csv fields so that i can adjust fields with that.I am getting csv fields like this :

$file = $path."/".$_FILES['file_uplod']['name'];
      $csv = new Varien_File_Csv();
      $data = $csv->getData($file);
for($i=1; $i<count($data); $i++){
  $name = $data[$i][1];

}

I want to code some generic so fields map with DB fields name.

4
  • If you can control how the csv looks like, a good idea would be to on the first row as column headers the attribute codes. This way you will know which is which. Something similar with how the default product import works.
    – Marius
    Jul 23, 2014 at 11:51
  • Sorry but i didn't get you.Like if i set header sku,name. How can i map that what is the position of these fields in the row ? got my point
    – Adda
    Jul 23, 2014 at 11:55
  • You could have the csv file like this. First row sku,name. Second row "some_sku","Some name". Then you will parse the csv and know that the first column (index 0) is the product sku and the second column (index 1) is the product name.
    – Marius
    Jul 23, 2014 at 12:02
  • means concatenation ? adding field name with value.any other alternative.this is good but i need more generic
    – Adda
    Jul 23, 2014 at 12:09

1 Answer 1

3

Not sure if this is an answer, but it looks ugly in the comments.
I don't know the code you use for import , but here is one that might work.
The code uses just 3 attributes but you can do the same for all of them.

The idea is to have the first row in the csv filled with attribute codes.
Here is an example:

sku        ,name        ,description
"some_sku" ,"Some name" ,"Some description"
"other_sku","Other name","Other description"

Now parse it like this:

$file = $path."/".$_FILES['file_uplod']['name'];
$csv = new Varien_File_Csv();
$data = $csv->getData($file);
//first line are the headers
$headers = $data[0];
$allProductData = array();
for($i=1; $i<count($data); $i++){
  $productData = array();
  foreach ($headers as $index=>$attributeCode) {
      $productData[$attributeCode] = $data[$i][$index];
  }
  $allProductData[] = $productData;
}

At the end, $allProductData should look like an array of arrays:

array(
    0 => array(
        'sku' => 'some_sku',
        'name' => 'Some Name',
        'description' => 'Some Description',
    );
    1 => array(
        'sku' => 'other_sku',
        'name' => 'Other Name',
        'description' => 'Other Description',
    );
);

You should be able to use this to import your data.

1
  • Great now i can get values by using the array key.thanks.
    – Adda
    Jul 24, 2014 at 5:29

Your Answer

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

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