Last active
August 29, 2015 14:25
-
-
Save wingsryder/8c9b23b41ec3dfa466d7 to your computer and use it in GitHub Desktop.
Add Attribute (DropDown) Value in magento via csv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add Attribute (DropDown) Options Value in magento via csv (http://prntscr.com/7wimts) | |
* CSV first column will have the values to import. | |
* @author Vikramjeet Thakur <[email protected]> | |
* | |
*/ | |
ini_set('max_execution_time', 0); | |
require_once 'app/Mage.php'; | |
umask(0); | |
Mage::app(); | |
function setOrAddOptionAttribute($arg_attribute, $arg_value) { | |
$attribute_model = Mage::getModel('catalog/resource_eav_attribute'); | |
$attribute_options_model = Mage::getModel('eav/entity_attribute_source_table'); | |
$attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute); | |
$attribute = $attribute_model->load($attribute_code); | |
$attribute_options_model->setAttribute($attribute); | |
$options = $attribute_options_model->getAllOptions(false); | |
// determine if this option exists | |
$value_exists = false; | |
foreach($options as $option) { | |
if ($option['label'] == $arg_value) { | |
$value_exists = true; | |
break; | |
} | |
} | |
// if this option does not exist, add it. | |
if (!$value_exists) { | |
$attribute->setData('option', array( | |
'value' => array( | |
'option' => array($arg_value,$arg_value) | |
) | |
)); | |
$attribute->save(); | |
} | |
} | |
$fileo = "trim.csv"; // filename and location | |
if (($handle = fopen($fileo, "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 1024, ",")) !== FALSE) { | |
try{ | |
setOrAddOptionAttribute('trim', $data[0]); | |
echo $data[0]."\r \n"; | |
}catch(Exception $e){ | |
//Mage::log($e->getMessage(), null, 'Failure.log'); // optional for magento logging | |
echo $e->getMessage(); | |
} | |
} | |
} | |
fclose($newhandle); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment