Get attribute admin option value if you have option Id or store view value
-
15-04-2021 - |
Вопрос
I'm trying since hours but I'm not able to load the admin option value of a certain attribute. Although I have enough information like option_id and store view value:
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','color');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
My attribute (name: color) option table looks like
Admin Default Store View 15 white 20 yellow 22 blue 45 green
Following gives me only the option ids:
$colorOfProduct = "white";
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','color');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();
foreach ($attributeOptions as $option) {
if ($option['label'] == $colorOfProduct)
echo $option['value'];
}
I have the Default store view value (i.e. white) and need the assigned Admin value. Unfortunately I don't get it done and appreciate every help.
Решение
For getting option value for admin
then you can get by below code and Where 0 is admin store id
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','color');
$collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->setStoreFilter(0)
->load();
echo "<pre>";
print_r($collection->toOptionArray());
echo "</pre>";
You need to just change store
view ids on place of 0;
Другие советы
Here my full solution for a certain problem. Perhaps someone can use it.
function getImgColor($productId, $colorOfProduct)
{
$optionId = Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'color', 1);
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','color');
$collection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($attributeId)
->setStoreFilter(0)
->load();
$collection = $collection->toOptionArray();
foreach ($collection as $option) {
if ($option['value'] == $optionId) {
$img = $option["label"];
break;
}
}
return file_exists(Mage::getBaseDir('media')."/colorall/_thumbs/color/".$img.".png") ? "<img src=\"".Mage::getBaseUrl('media')."colorall/_thumbs/color/".$img.".png\" title=\"".$colorOfProduct."\" />" : Mage::getBaseDir('media')."/colorall/_thumbs/color/".$img.".png";
}
You can do this more simply in a single call as follows:
Example attribute 'gender' to get custom attribute value for and we are using the magic method getGender() to do the equivalent of getData('gender')
$gender = Mage::getModel('catalog/resource_eav_attribute')->loadByCode(Mage_Catalog_Model_Product::ENTITY, 'gender')->getSource()->getOptionText($product->getGender());
set store id before retriving value
$attribute->setStoreId(\Magento\Store\Model\Store::DEFAULT_STORE_ID)->getFrontend()->getValue($product)