php - Setting Magento dropdown (select) value for product -
i have created quite big import script importing products csv magento. have 1 remaining issue resolve.
i use dropdowns attributes. unfortunately can't set values attributes single product. did:
- created attribute set [php],
- added dropdown attribute values set [php],
- added new product in proper attribute set , tried set value attribute have created.
i tried few methods, here one looking 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 option exists $value_exists = false; foreach($options $option) { if ($option['label'] == $arg_value) { $value_exists = true; break; } } // if option 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 don't work. ideas? i'm using magento 1.7.0.2
you can this:
$attr_id = $this->attributevalueexists('manufacturer', 'samsung'); $product->setdata('manufacturer', $attr_id ); $product->save(); public function attributevalueexists($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_table = $attribute_options_model->setattribute($attribute); $options = $attribute_options_model->getalloptions(false); foreach($options $option) { if ($option['label'] == $arg_value) { return $option['value']; } } return false; }
let me know if have questions.
cordially.
Comments
Post a Comment