Tag Archives: attributes

Getting value of Attribute Option and adding a new Attribute Option in Magento

This, I find, is a hard one to come across. At least, trying to find a sensible solution that just spits out a numerical value which, more often than not, is all you want. Needing to get the value of an option from a multi-option/dropdown attribute. Try this:

public function getAttributeOptionValue($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 as $option) {
        if ($option['label'] == $arg_value) {
            return $option['value'];
        }
    }
 
    return false;
}

And use it as such:

$optionValue = $this->getAttributeOptionValue("size", "XL");

where “size” is the attribute name/ID in the database, and “XL” is the exact label belonging to the option you need to get. Likewise, if you need to add a new option to the attribute’s list, use this:

public function addAttributeOption($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);
 
    $value['option'] = array($arg_value,$arg_value);
    $result = array('value' => $value);
    $attribute->setData('option',$result);
    $attribute->save();
 
    return $this->getAttributeOptionValue($arg_attribute, $arg_value);
}

and use as such:

$optionValue = $this->addAttributeOption("size", "XXL");

again, where “size” is the attribute name/ID, and “XXL” is the label of the new option required. It returns the value of the option it just added incase it’s needed. Bear in mind that this function doesn’t first check to see if the label already exists as an option, so you should probably use $this->getAttributeOption() first and only run $this->addAttributeOption() upon a false return.

It’s probably not the most efficient way of doing it, but if efficiency is what you really need, $options could be cached/saved so that the DB/model is only every queried once across both functions within the same model.

Adding Multi-select Attributes using an Installer in Magento (Part 2)

So in my last post, I talked about adding an attribute using an installer, as opposed to adding it through the admin panel. This would give you the ability to enforce the addition of an attribute if your code depends on it. However, my last code snippet adds the given attribute as a system attribute, thus “infecting” every attribute set, and not making it removable (or making it a right nightmare to remove from all sets except the one you want). Anyway, here’s a resolution.

Basically, it involves having to override and extend Mage_Eav_Model_Entity_Setup which contains the addAttribute method. This, by default (and is not configurable, for some reason) adds the new attribute to every set. Here is how to override that behaviour and allows you to add the attribute to one set and one set only.

Continue reading “Adding Multi-select Attributes using an Installer in Magento (Part 2)” »

Adding Multi-select Attributes using an Installer in Magento

I had to do a modification for a Magento install that required the use of adding a multi-select attribute. Most other people would just add the attribute by hand and forget about it, but I’m one for making the upgrade process easy. However, the Magento developers don’t seem to want you to know about it (or anything else for that matter, given the lack of documentation!!). Anyway, I eventually stumbled across a small snippet of code, and after a bit of tweaking to figure out where all the parameters went, the installer worked. Easy upgrading :)

// File: app/code/local/MyCompany/Catalog/sql/mysql4-install-0.1.php
// OR
// File: app/code/local/MyCompany/Catalog/sql/mysql4-upgrade-0.1-0.2.php
 
$installer = $this;
$installer->startSetup();
 
$installer->addAttribute('catalog_product', 'attr_id',array(
			 'label'             => 'Frontend Name',
			 'type'              => 'varchar',
			 'input'             => 'multiselect',
			 'backend'           => 'eav/entity_attribute_backend_array',
			 'frontend'          => '',
			 'source'            => '',
			 'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
			 'visible'           => true,
			 'required'          => false,
			 'user_defined'      => false,
			 'searchable'        => false,
			 'filterable'        => false,
			 'comparable'        => false,
			 'option'            => array (
			 	'value' => array('optionone' => array('First Option'),
			 			 'optiontwo' => array('Second Option'),
			 			 'optionthree' => array('Third Option'),
			 			 )
			 			),
			 'visible_on_front'  => false,
			 'visible_in_advanced_search' => false,
			 'unique'            => false
));
 
$installer->endSetup();

Of course, ‘addAttribute’ isn’t a method of $installer (or $this if you’re running a standard Mysql4 setup). So you have to extend a different setup with addAttribute defined. I found this one works well:

// File: app/code/local/MyCompany/Catalog/Model/Mysql4/Setup.php
 
class MyCompany_Catalog_Model_Mysql4_Setup extends Mage_Sales_Model_Mysql4_Setup {}

One thing to note with this however, is that the attribute will end up as a System attribute, and applied to every attribute set on the system. I’m currently working on a way around this, and will edit this post when I have something more to share. :)