0

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.

0

Should just be this

$attribute_code = 'product_type';
echo $_product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($_product);

or maybe

echo $_product->getAttributeText('product_type');
5
  • The first code returns No and second one empty, This code also works for product text attribute.
    – fernandus
    May 22 '19 at 13:16
  • $_product->getResource()->getAttribute('product_type')->getFrontendLabel() works and in product detail in more information i can see the value i have selected for code 'product_type' but dont know why this code is not working
    – fernandus
    May 22 '19 at 13:19
  • I have updated my question
    – fernandus
    May 22 '19 at 13:32
  • ->setStoreId(0) will get data based on store few blogs has this, By issue is different.
    – fernandus
    May 22 '19 at 13:39
  • I am not getting for color dropdown attribute in too
    – fernandus
    May 22 '19 at 14:26
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.