2

How to show product attribute value in order detail in Magento 2.2.6? I already tried below code but not working:

File Path:

public_html/vendor/magento/module-sales/view/adminhtml/templates/items/column/name.phtml

$_item->getProduct()->getAttributeText('attribute_code');
$_item->getProduct()->getProductAttribute('attribute_code');

Need Help.

enter image description here

2 Answers 2

3

You can get product attribute value using $objectmanager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productId = $_item->getProduct()->getId();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
echo $product->getData('attribute_code');

You can also try this code as well ( For select option attribute )

echo $product->getResource()->getAttribute('attribute_code')->getFrontend()->->getValue($product);
8
  • How to check if attribute value is not empty then echo? Jan 25, 2019 at 9:19
  • Above code not working. Jan 25, 2019 at 9:37
  • Please check my answer. Jan 25, 2019 at 9:59
  • Is this code snippet also work for front end view or just admin? Jan 25, 2019 at 10:32
  • Tried same code but it break site front end. Jan 25, 2019 at 10:48
0

@Aasim answer works.I added below code to get attribute value.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $productId = $_item->getProduct()->getId();
            $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
            $value = $product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($product);
            if ($value) {
                echo '<p><strong>'.$value.'</strong></p>';
            } 
2
  • Please accept my ans and upvote👍 if its useful to you!! thanks :) Jan 30, 2019 at 9:17
  • $_item variable, how rendering the value. You will get undefined variable issue
    – Jdprasad V
    Jun 10, 2020 at 10:07

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.