Magento 2.3 how to get attribute options label by value in block
-
18-03-2021 - |
سؤال
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.
المحلول
Use getAttributeText()
function.
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
$colrValue = $product->getAttributeText("color");
}
نصائح أخرى
You can try this code.
$productCollection = $this->getProductCollection();
foreach ($productCollection as $product) {
$product->getResource()->getAttribute('color')->getFrontend()->getValue($product);
}
Hope this will help you!
لا تنتمي إلى magento.stackexchange