How to get all options for all store views for a single attribute?
-
16-10-2019 - |
Question
I am trying to retrieve all options for all store views for a single attribute e.g. color
.
For the attribute color
I have created two options blue
and white
. I have tried the following code that is suppose to return all the options labels for all store views, but only returns the admin
option label for me.
$option_arr = array();
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
foreach ($attribute->getSource()->getAllOptions(false) as $option) {
$option_arr[$option['value']] = $option['label'];
}
// $option_arr contains Array([4] => Blue, [3] => White)
The following works fine for getting all the attribute color
titles for each store view, but does not work for options.
$product = Mage::getModel('catalog/product')->load();
$attribute_title = $product->getResource()->getAttribute('color');
// $attribute_title contains Array([1] => ~~~, [2] =>Color, [3] => Couleur, [4] => Còôlòôr)
Solution
/**
* @var $config Mage_Eav_Model_Config
* @var $options Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection
*/
$storeId = 3;
$config = Mage::getModel('eav/config');
$attribute = $config->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'color');
$values = $attribute->setStoreId($storeId)->getSource()->getAllOptions();
print_r($values);
//here is another method
$options = Mage::getResourceModel('eav/entity_attribute_option_collection');
$values = $options->setAttributeFilter($attribute->getId())->setStoreFilter($storeId)->toOptionArray();
print_r($values);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange