I created a fairly large import script that imports products from CSV to magento. I have one remaining problem to solve.
I use dropdown lists for attributes. Unfortunately, I cannot set values ββfor these attributes for a single product. What I've done:
- created attribute set [php],
- added dropdown attribute with values ββfor this set [php],
- added a new product to the corresponding set of attributes and tried to set the value for the created attribute.
I tried several methods, here is one that looks good to me:
private function setOrAddOptionAttribute($product, $arg_attribute, $arg_value) { $attribute_model = Mage::getModel('eav/entity_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(); } $product->setData($arg_attribute, $arg_value); $product->save(); }
Unfortunately this does not work. Any ideas? I am using Magento 1.7.0.2
source share