How to show the drop down attribute value using getData() method in magento 2?
Question
When I try to get data from the material attribute like $_product->getData('material')
, it shows the integer value. How can I convert it to the actual front-end value and in which file do I have to make the changes?.
Solution
Instead of getData()
you need to use getAttributeText('attribute_code')
for dropdown attributes. like
$_product->getAttributeText('material');
For multi-select try this :
$result = $_product->getResource()->getAttribute('material')->getFrontend()->getValue($_product);
OTHER TIPS
To fetch value of attribute Use:
$this->getProduct()->getAttributeText('material');
Or
$this->getProduct()->getResource()->getAttributeRawValue($this->getProduct()->getId(),'material',$this->_storeManager->getStore()->getId());
You can get product attribute option values by below code
$_product->getAttributeText('material');
You can get multi select option values by
$_attributeValue = $block->getProduct()->getResource()->getAttribute('sizing_guidelines')->getFrontend()->getValue($block->getProduct());
$_product->getAttributeText('material');
is the right answer --- beware that $_product->getResource() is a deprecated function
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange