1

I would like to know how to add an option to a product. I created the attribute via an sql file and it works.

$installer->addAttribute('catalog_product', 'score_attribute',  array(
    'group'             => 'General',
    'label'             => 'Score Attribute',
    'type'              => 'int',
    'input'             => 'select',
    'default'           => '0',
    'class'             => '',
    'backend'           => '',
    'frontend'          => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => true,
    'user_defined'      => false,
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'wysiwyg_enabled'   => false,
    'is_html_allowed_on_front'      => false,
    'visible_in_advanced_search'    => false,
    'filterable_in_search'          => false,
    'used_in_product_listing'       => true,
    'used_for_sort_by'              => true,
    'is_configurable'               => false,
    'used_for_promo_rules'          => true,
    'unique'                        => false,
    'note'                          => 'Product score' ));

Options are created dynamically. An option contains a score which can change, depends of a stock and several other things. This option is correctly created and i can see it on EAV_Attribute_option_value.

 $attributeCode = 'score_attribute';
 $attribute = Mage::getModel('eav/entity_attribute')
      ->loadByCode('catalog_product', $attributeCode);
 $option['attribute_id'] = $attribute->getId();
 $option['values'] =  array($score);
 $installer->addAttributeOption($option);

But now i don't know how to associate it to a product. I tried to do this but nothing happens :

 $product->setData('score_attribute',$optionId); or 
 $product->setData('score_attribute', $score);

Could you help me please?

Cheers,

0

You can do that :

if ($product->getData('score_attribute') != $optionId) 
       $product->setData('score_attribute', $optionId)->getResource()->saveAttribute($product, 'score_attribute');

Good luck

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.