Magento assign attribute to attribute set

This is how to assign an existing attribute to an existing attribute set


$setup = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$setup->startSetup();

$attribute_set_name = ATTRIBUTE_SET_VISIT;
$attribute_code = $attr;
$group_name = GROUP_ADIN;

$attribute_set_id=$setup->getAttributeSetId('catalog_product', $attribute_set_name);
$attribute_group_id=$setup->getAttributeGroupId('catalog_product', $attribute_set_id, $group_name);
$attribute_id=$setup->getAttributeId('catalog_product', $attribute_code);

$setup->addAttributeToSet($entityTypeId='catalog_product',$attribute_set_id, $attribute_group_id, $attribute_id);

Magento create dropdown from product attribute option

Here a simple but complete example to create a dropdown list for a product attribute

//retrieve informations
$attribute_model        = Mage::getModel('eav/entity_attribute');
$attribute_code         = $attribute_model->getIdByCode('catalog_product', 'audience');
$attribute              = $attribute_model->load($attribute_code);
$attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;
$attribute_table        = $attribute_options_model->setAttribute($attribute);
$options                = $attribute_options_model->getAllOptions(false);

//display them
echo '<select id="audience" class="select" name="audience">';
foreach($options as $option)
{
    echo '<option value="'.$option['value'].'">'.$helper->__($option['label']).'</option>';
}
echo '</select>';

Magento – update attribute parameters programmatically

This is how you can update attribute parameters, the one saved in catalog_eav_attribute table

In this exemple we will enable the “used_in_product_listing” params of the “code_catalogue” attribute

$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','code_catalogue');
if ($attributeId) {
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
    $attribute->setUsedInProductListing(1)->save();
}

And that’s all.

Magento : list all values of an attribute

You want to see all values of an attribute, for debug purpose or something else ?

$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr_model->load(959); // you can find the attribute_id on the eav_attribute table
$options = $attr_model->getSource()->getAllOptions(false);
foreach($options as $value)
{
     print_r($value);
     echo "<br />";
}

You will get something like that :

Array
(
    [value] => 154
    [label] => 2008
)
Array
(
    [value] => 155
    [label] => 2009
)
Array
(
    [value] => 156
    [label] => 2010
)