First off I'm new to Magento, so please forgive me for my ignorance.

I would like to read from a CSV file that has item_id, sku, and the custom options data so I can use it in a foreach loop.

I'm not looking for a complete solution, just an idea of what I can/should do. All I've done thus far is grab the CSV file and create a new instance of the Varien_File_Csv class. After that, I have a separate foreach loop but it requires me to enter the product_id's one by one. I would like to create an empty array that will be filled with the data in the CSV, I just don't know which methods to use. Can anyone advise a little bit?

error_reporting(E_ALL | E_STRICT);
require_once 'app/Mage.php';

$file = 'test.csv';
$csv = new Varien_File_Csv();
$data = $csv->getData($file);

$productIds = array(100, 101, 102);

$option = array(
    'title' => 'Test Option',
    'type' => 'file',
    'is_require' => 1,
    'price' => 10,
    'price_type' => 'fixed',
    'sku' => 'testsku',
    'file_extension' => 'png,jpg',
    'image_size_x' => '100',
    'image_size_y' => '200'

foreach ($productIds as $productId) {
    $product = Mage::getModel('catalog/product')->load($productId);
    $optionInstance = $product->getOptionInstance()->unsetOptions();

    if (isset($option['is_require']) && ($option['is_require'] == 1)) {
  • So you just want to add new options/ wipe out old options, from a given product ID that you'd fetch from a CSV file? Or am I getting it wrong? Mar 25, 2014 at 13:14
  • @JulienLachal: Yes, except I don't want to wipe out existing options (as the products don't currently have options).
    – Mystic
    Mar 25, 2014 at 14:27

2 Answers 2


Regardless of the specifics of the implementation, you should be using Magento's Dataflow (Advanced Profiles) for this. It handles the implementation of reading the CSV and saving the product for you. You only need to extend Mage_Catalog_Model_Convert_Adapter_Product and implement your logic in there. Your Profile Actions XML should look something like this:

<action type="dataflow/convert_adapter_io" method="load">
    <var name="type">file</var>
    <var name="path">var/import</var>
    <var name="filename"><![CDATA[products.csv]]></var>
    <var name="format"><![CDATA[csv]]></var>

<action type="dataflow/convert_parser_csv" method="parse">
    <var name="delimiter"><![CDATA[,]]></var>
    <var name="enclose"><![CDATA["]]></var>
    <var name="fieldnames">true</var>
    <var name="store"><![CDATA[0]]></var>
    <var name="number_of_records">1</var>
    <var name="decimal_separator"><![CDATA[.]]></var>
    <var name="adapter">mymodule/convert_adapter_product</var>
    <var name="method">parse</var>

Likely you are doing more work than you need to. You should look into using Magmi (http://sourceforge.net/projects/magmi/) and do it with that. It can handle custom options (you would have to build out that functionality on your own) http://wiki.magmi.org/index.php?title=Custom_Options and you can automate it if needed (http://wiki.magmi.org/index.php?title=Main_Page#Automating_magmi_calls).

If you are looking to learn more about programming for Magento, then look into the Data Profiles and writing custom models/controllers. If you need to get something done, use Magmi.

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.