When adding the product attribute, set used_in_product_listing
, used_for_sort_by
and filterable
to true
.
You can do that with the following update script:
$installer = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();
$installer->updateAttribute('catalog_product', 'product_type', 'used_in_product_listing', 1);
$installer->updateAttribute('catalog_product', 'product_type', 'used_for_sort_by', 1);
$installer->updateAttribute('catalog_product', 'product_type', 'is_filterable', 1);
$installer->endSetup();
Thats all, if if you are NOT using the flat catalog.
If you enable the flat catalog product feature, then you need to ensure that your custom source model implements the methods getFlatColums()
, getFlatIndexes()
and getFlatUpdateSelect()
.
If they are missing, your attribute won't be included in the generated flat tables, even if used_in_product_listing
is enabled.
Have a look at the class Mage_Eav_Model_Entity_Attribute_Source_Boolean
for an example. You can probably copy over the methods with minimal adjustments into your custom source model from there. Otherwise, please ask a new question.
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
You'll need to get a collection of the desired products and iterate through it to get the attribute values. Like this..