Magento 2.3 how to get attribute options label by value in block
-
18-03-2021 - |
Question
I am getting attribute values of an attribute color as 5567, 5568, 5569 etc in block of my custom module.
I want to get the attribute color instead of values as red, blue.
I am getting values as below -
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
$colrValue = $product->getColor();
}
Please help.
Solution
Use getAttributeText()
function.
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
$colrValue = $product->getAttributeText("color");
}
OTHER TIPS
You can try this code.
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
$product->getResource()->getAttribute('color')->getFrontend()->getValue($product);
}
Hope this will help you!
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange