2

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?

<?php
error_reporting(E_ALL | E_STRICT);
require_once 'app/Mage.php';
Mage::app('default');


$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();

    $product->setHasOptions(1);
    if (isset($option['is_require']) && ($option['is_require'] == 1)) {
        $product->setRequiredOptions(1);
    }
    $optionInstance->addOption($option);
    $optionInstance->setProduct($product);
    $product->save();
}
  • 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? – Julien Lachal Mar 25 '14 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 '14 at 14:27
1

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>

<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>
</action>
0

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, privacy policy and cookie policy

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