Like all of Magento, the answer's in the source — all you need to do is go digging (or hop to then end of this post).
First, find the class for the attribute source
$source = Mage::getResourceModel('catalog/product')
->getAttribute('color')
->getSource();
var_dump(get_class($source));
exit;
In modern versions of Magento, this should point to the class Mage_Eav_Model_Entity_Attribute_Source_Table
, which is located at
#File: app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Table.php
class Mage_Eav_Model_Entity_Attribute_Source_Table extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
//...
}
Next, we'll look for the definition of the getOptionId
method on this class and/or it's parent classes. We'll find it in the Mage_Eav_Model_Entity_Attribute_Source_Abstract
class
#File: app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Abstract.php
public function getOptionId($value)
{
foreach ($this->getAllOptions() as $option) {
if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
return $option['value'];
}
}
return null;
}
Examining this method, we can see if works by foreach
ing over a list of options from the getAllOptions
method. So let's look at this method's definition.
#File: app/code/core/Mage/Eav/Model/Entity/Attribute/Source/Table.php
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$storeId = $this->getAttribute()->getStoreId();
if (!is_array($this->_options)) {
$this->_options = array();
}
if (!is_array($this->_optionsDefault)) {
$this->_optionsDefault = array();
}
if (!isset($this->_options[$storeId])) {
$collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($this->getAttribute()->getId())
->setStoreFilter($this->getAttribute()->getStoreId())
->load();
$this->_options[$storeId] = $collection->toOptionArray();
$this->_optionsDefault[$storeId] = $collection->toOptionArray('default_value');
}
$options = ($defaultValues ? $this->_optionsDefault[$storeId] : $this->_options[$storeId]);
if ($withEmpty) {
array_unshift($options, array('label' => '', 'value' => ''));
}
return $options;
}
Ah ha! Now we're on to something. This appears to be the method that loads all the option information. Specifically, we're interested in
$storeId = $this->getAttribute()->getStoreId();
So, Magento gets the store ID for the options to load from the attribute. This means you should be able to do something like the following (replacing color
and red
with your own variables, of course)
//get the attribute
$attribute = Mage::getResourceModel('catalog/product')
->getAttribute('color');
//set the store id on the attribute
$attribute->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID);
//get the source
$source = $attribute->getSource();
//get the id
$id = $source->getOptionId('red');
Got solution... Hurreee...:-)
->addFieldToFilter('diy_kit_price',
array(array('from'=>'0','to'=>'5000'),
array('from'=>'15000','to'=>'20000'))
)
Best Answer
I managed to get this to work using Marius advice and using the answer from the post below:
Collection with FIND_IN_SET on multiselect attributes, but some has only one value
My code now reads