Magento 2.3 how to get attribute options label by value in block
-
18-03-2021 - |
Domanda
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.
Soluzione
Use getAttributeText()
function.
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
$colrValue = $product->getAttributeText("color");
}
Altri suggerimenti
You can try this code.
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
$product->getResource()->getAttribute('color')->getFrontend()->getValue($product);
}
Hope this will help you!
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange