How to get dropdown product attribute value Magento2
-
02-03-2021 - |
Question
I have created a product drop-down attribute and assigned a value to it on the product.
I need them on product detail page.
$_attributeValue = $_product->getResource()->getAttribute('product_type')->setStoreId(0)->getFrontend()->getValue($_product);
echo $_attributeValue;
OR
$_attributeValue = $_product->getResource()->getAttribute('product_type')->getFrontend()->getValue($_product);
echo $_attributeValue;
Output as
NO
On print($_attributeValue)
my output
Magento\Framework\Phrase Object ( [text:Magento\Framework\Phrase:private] => No [arguments:Magento\Framework\Phrase:private] => Array ( ) )
But for product text attribute I get the output.
Solution
Should just be this
$attribute_code = 'product_type';
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);
or maybe
echo $_product->getAttributeText('product_type');
OTHER TIPS
I tried to get the product from item which dint work for me
$product = $_item->getProduct();
$product->getResource()->getAttribute('product_type')->getFrontend()->getValue($product);
This one works
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($_item->getProductId());
$product_type = $product->getResource()->getAttribute('product_type')->getFrontend()->getValue($product);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange