Monthly Archives: January 2011

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.

Amazon S3 CDN distribution for Menalto Gallery 3 v2.1 released

Announcing the release of the Amazon S3 v2.1 module for Gallery 3. A few changes are presented here;

  • Firstly, started using double digits for version numbers so I can create minor revisions
  • Added fields to database to store MD5 hashes of files. These are used to match the MD5 hash of the local file, and what the module thinks is uploaded, and also for comparison with S3 itself (which provides an MD5 hash when asked for bucket info)
  • Re-visited re-sync task. Taken out “emptying bucket” code (as there’s really no need it seems), and simplified the whole process, allowing the module to compare the MD5 hash of the local file against the same file path on S3, and upload/overwrite if they’re different, or if the module doesn’t believe the file has been uploaded.
  • Resolved issues of moving entire albums from one location to another and files disappearing. This invoked a re-visit of the move code as well and cleaned up quite a lot of unnecessary stuff.

Download the latest version here: Amazon S3 for Gallery 3 : v2.1 (md5: )
As always, direct comments, feedback and bug reports to the module‚Äôs official thread on Gallery’s forums.

Note: I will no longer be updating the gallery3-contrib git repository as it’s just easier to update my site than it is to update a load of other places and wait for pulls to be granted. Sorry.

Amazon S3 CDN distribution for Menalto Gallery 3 v2 released

Well, after a few weeks of having this module used by many in the community as a beta module, and due to lack of bug reports since, I’m now convinced it’s stable enough to release. So, here it is.

Amazon S3 for Gallery 3 : v2.1 (md5: )

As always, direct comments, feedback and bug reports to the module’s official thread on Gallery’s forums.

That is all :)