I am currently developing a product configurator. I am submitting a form which sends the admin option values of my custom attributes via POST to a controller action. If I use the current store's attribute option labels all is working just fine, the product collection is filtered correctly and a simple product which is associated to a configurable product is returned.
Sadly I am using the admin values for form and URL generation because the default swedish store values contain too many special characters.
So my question is: how do I get the right product from my collection when using the admin option values or more specific, how do I get the admin option value's ID like using this for the default store's values:
$id = Mage::getResourceModel('catalog/product')
->getAttribute($key)
->getSource()
->getOptionId($value);
I already tried filtering by addStoreFilter(0), but that didn't work either. I already read that it could have something to do with flat tables being used in default store but not for admin, but I am rather unexperienced with Magento, so I don't really understand the difference using flat tables and what that really means.
Here is my last question concerning the filtering for reference:
addFieldToFilter doesn't return the right product for selected attribute values
Here is my getProduct() function:
public function getProduct($attributes)
{
Mage::Log($attributes);
$productModel = Mage::getModel('catalog/product');
//Get Product Collection
$collection = $productModel->getCollection()
//Filter for Selected Product
$collection->addAttributeToSelect('status');
$collection->addAttributeToSelect('doorconfig_enable');
$collection->addAttributeToFilter('doorconfig_enable',array('eq' => 1));
foreach ($attributes as $key => $value)
{
$collection->addAttributeToSelect($key);
$id = Mage::getResourceModel('catalog/product')
->getAttribute($key)
->getSource()
->getOptionId($value);
$collection->addAttributeToFilter($key,
array(
'eq' => $id
)
);
}
$selection = $collection->getSelect()->__toString();
Mage::Log($selection);
Mage::log($collection->getSize(),null,'custom.log');
$product = $collection->getFirstItem();
return $product;
}
Best Answer
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
In modern versions of Magento, this should point to the class
Mage_Eav_Model_Entity_Attribute_Source_Table
, which is located atNext, we'll look for the definition of the
getOptionId
method on this class and/or it's parent classes. We'll find it in theMage_Eav_Model_Entity_Attribute_Source_Abstract
classExamining this method, we can see if works by
foreach
ing over a list of options from thegetAllOptions
method. So let's look at this method's definition.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
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
andred
with your own variables, of course)