This is a classic case where code-generation is your friend. Stop creating these by hand, and try using the following free and open source script, (which has also been incorporated into the n98-magerun
tool)
For example, the following will duplicate the sample data's color attribute
$ magento-create-setup.php color
//WARNING, non false value detected in is_used_for_price_rules. The setup resource migration scripts may not support this (per 1.7.0.1)
<?php
if(! ($this instanceof Mage_Catalog_Model_Resource_Setup) )
{
throw new Exception("Resource Class needs to inherit from " .
"Mage_Catalog_Model_Resource_Setup for this to work");
}
$attr = array (
'attribute_model' => NULL,
'backend' => '',
'type' => 'int',
'table' => '',
'frontend' => '',
'input' => 'select',
'label' => 'Color',
'frontend_class' => '',
'source' => '',
'required' => '0',
'user_defined' => '1',
'default' => '',
'unique' => '0',
'note' => '',
'input_renderer' => NULL,
'global' => '1',
'visible' => '1',
'searchable' => '1',
'filterable' => '1',
'comparable' => '1',
'visible_on_front' => '0',
'is_html_allowed_on_front' => '0',
'is_used_for_price_rules' => '1',
'filterable_in_search' => '1',
'used_in_product_listing' => '0',
'used_for_sort_by' => '0',
'is_configurable' => '1',
'apply_to' => 'simple',
'visible_in_advanced_search' => '1',
'position' => '1',
'wysiwyg_enabled' => '0',
'used_for_promo_rules' => '1',
'option' =>
array (
'values' =>
array (
0 => 'Green',
1 => 'Silver',
2 => 'Black',
3 => 'Blue',
4 => 'Red',
5 => 'Pink',
6 => 'Magenta',
7 => 'Brown',
8 => 'White',
9 => 'Gray',
),
),
);
$this->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'color', $attr);
If you're using the n98-magerun
version, that'd be
$ n98-magerun dev:setup:script:attribute catalog_product color
Using code generation will let you get your job done quicker, and as time goes on you'll start to learn the format.
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');
Best Answer
Try this: