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');
In config.xml,
<global>
<events>
<admin_system_config_changed_section_design>
<observers>
<custom>
<type>singleton</type>
<class>Custon_Custom_Observer</class>
<method>saveSystemConfig</method>
</custom>
</observers>
</admin_system_config_changed_section_design>
</events>
</global>
In observer.php,
public function saveSystemConfig(Varien_Event_Observer $observer)
{
$postData = $observer->getEvent()->getData();
if (is_null($postData['store']) && $postData['website']) //check for website scope
{
$scopeId = Mage::getModel('core/website')->load($postData['website'])->getId();
$description = Mage::app()->getWebsite($scopeId)->getConfig('design/custom/description');
$currentScope = 'websites';
}
elseif($postData['store']) //check for store scope
{
$scopeId = Mage::getModel('core/store')->load($postData['store'])->getId();
$description = Mage::app()->getStore($scopeId)->getConfig('design/custom/description');
$currentScope = 'stores';
}
else //for default scope
{
$scopeId = 0;
$description = Mage::getStoreConfig('design/social-meta-tags/design/custom/description')
$currentScope = 'default';
}
}
From the above you can get individual field values in all scopes.
Best Answer
The fact that
0
is used is either an inconsistency or is to make the code easier to read.So somone got tired of adding the constant name
Mage_Core_Model_App::ADMIN_STORE_ID
or decided it is more readable to have 0 almost everywhere.This is just my opinion. I could be wrong.
But one think is clear.
The admin store view will always have the id
0
, unless you decide to change that id for some reason. Then it will all blow up.In the
install-1.6.0.0.php
file in theMage_Core
module there is this:This inserts the admin store in the db.
There are similar pieces of code above that one that insert a website with id 0 and a store group.
There are also similar inserts in the
mysql4-install-0.8.0.php
file for versions previous to 1.6.In conclusion, it's safe to say that the admin store view will always have the id 0. Otherwise nothing will work.
Here is a random method I found in the store collection model:
From here I can conclude that default (or admin) means
0
.